问题
Whenever I try to plot my xts object with values passed for parameter col and lwd, it returns an error saying that the parameter col (and then lwd if I comment out col) matched multiple values. Here is what the function looks like -
data is an xts object
plot(data, lwd=2, col="red")
Installing xtsExtra seems to solve the problem, but I don't understand why.
回答1:
xts:::plot.xts
specifies several parameters when constructing the axes (col
, lwd
, las
, and mgp
), but it also passes ...
to the axis
call. For example:
axis(1, at = xycoords$x, labels = FALSE, col = "#BBBBBB", ...)
col
is already specified, so if you specify it also, it gets passed to axis
via ...
and now there are two col
arguments specified to the axis
call.
This isn't a problem with xtsExtra:::plot.xts
because those parameters are removed from ...
before ...
is passed to the axis
call.
回答2:
My solution to this to pass type='n'
and then call lines
:
require(quantmod)
getSymbols('CPIAUCSL',src='FRED')
xts::plot.xts(CPIAUCSL, type='n')
lines(CPIAUCSL, lwd=3, col='darkgoldenrod')
This does not require the xtsExtra
package, which makes other changes to the plot and is (maybe) not being actively maintained. (At least it doesn't install via the package manager, today, on R 3.1.1.)
来源:https://stackoverflow.com/questions/16473411/plotting-xts-objects-passsing-values-for-lwd-and-col-parameters-creating-error