问题描述
我想在某个图表上的多个日期添加垂直线.到目前为止,我还没有设法完成这个简单的任务.这是我尝试过的:
I want to add vertical lines on several dates on a certain graph. So far I haven't managed to achieve this simple task. This is what I tried:
> s <- get(getSymbols('nvmi'))["2012::"]
> d1 <- index(s[100])
> d1
[1] "2012-05-24"
> chart_Series(s,TA="addLines(v=d1)")
Error in get.current.chob() : improperly set or missing graphics device
> chart_Series(s)
> abline(v=d1)
# nothing
> add_TA("addLines(v=d1")
Error in `[.data.frame`(lenv$xdata, Env$xsubset) :
undefined columns selected
根据我在此处阅读的内容,我知道 abline
不应该与新的 chart_Series
函数一起使用.无论如何它似乎不起作用.addLines
函数在我尝试过的任何形式中都不起作用 - 普通 addLines
、plot(addLines(...))
、chart_Series(..., TA="addLines(...)")
或 add_TA("addLines(...)")
.
From what I have already read here, I know that abline
is not supposed to work with the new chart_Series
function. It doesn't seem to work anyway. The addLines
function does not work in any of the forms I tried - plain addLines
, plot(addLines(...))
, chart_Series(..., TA="addLines(...)")
or add_TA("addLines(...)")
.
我需要使用 quantmod 的实验版本,因为它解决了我在旧版本中遇到的其他问题.d1
最终会是一个日期列表.
I need to use the experimental version of quantmod because it solved other problems I had with the old version. d1
would eventually be a list of dates.
推荐答案
您不能混合使用 quantmod 图表函数的新旧版本中的函数.如果要使用addLines
,则必须使用chartSeries
.即使您使用 addLines
和 chartSeries
,d1
也应该是 xts 对象,而不是日期时间对象.例如:
You can't mix functions from the old and new versions of quantmod's charting functions. If you want to use addLines
, you have to use chartSeries
. Even if you use addLines
and chartSeries
, d1
should be an xts object, not a datetime object. For example:
library(quantmod)
data(sample_matrix)
s <- as.xts(sample_matrix)
chartSeries(s,TA="addLines(v=s[100])")
如果您想使用 chart_Series
添加一条垂直线,请使用 TRUE
值创建一个逻辑 xts 对象,您希望这些线出现在其中,FALSE代码> 否则.例如:
If you want to add a vertical line using chart_Series
, create a logical xts object with TRUE
values where you want the lines to appear and FALSE
otherwise. For example:
l <- xts(!as.logical(s[,1]),index(s))
l[100] <- TRUE
chart_Series(s,TA="add_TA(l,on=1)")
另请注意,您可以在 add_TA
调用中使用 on=-1
将垂直线放在图表后面":
Also note that you can put the vertical line "behind" the chart by using on=-1
in the add_TA
call:
chart_Series(s,TA="add_TA(l,on=-1,col='grey',border='grey')")
这篇关于向 quantmod::chart_Series 添加垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!