[英]Add vertical lines to quantmod::chart_Series
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
From what I have already read here, I know that abline
is not supposed to work with the new chart_Series
function. 根据我在这里已经阅读的内容,我知道abline
不适用于新的chart_Series
函数。 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(...)")
. addLines
函数不能用于我尝试的任何形式 - plain addLines
, plot(addLines(...))
, chart_Series(..., TA="addLines(...)")
或add_TA("addLines(...)")
。
I need to use the experimental version of quantmod because it solved other problems I had with the old version. 我需要使用quantmod的实验版本,因为它解决了旧版本的其他问题。 d1
would eventually be a list of dates. d1
最终将成为日期列表。
You can't mix functions from the old and new versions of quantmod's charting functions. 您无法混合新版本的quantmod图表功能中的功能。 If you want to use addLines
, you have to use chartSeries
. 如果要使用addLines
,则必须使用chartSeries
。 Even if you use addLines
and chartSeries
, d1
should be an xts object, not a datetime object. 即使您使用addLines
和chartSeries
, d1
应该是xts对象,而不是datetime对象。 For example: 例如:
library(quantmod)
data(sample_matrix)
s <- as.xts(sample_matrix)
chartSeries(s,TA="addLines(v=s[100])")
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. 如果要使用chart_Series
添加垂直线,请创建一个逻辑xts对象,其中TRUE
值需要显示行,否则为FALSE
。 For example: 例如:
l <- xts(!as.logical(s[,1]),index(s))
l[100] <- TRUE
chart_Series(s,TA="add_TA(l,on=1)")
Also note that you can put the vertical line "behind" the chart by using on=-1
in the add_TA
call: 另请注意,您可以通过在add_TA
调用中使用on=-1
将垂直线放在图表的“后面”:
chart_Series(s,TA="add_TA(l,on=-1,col='grey',border='grey')")
add horizontal line my example: 添加水平线我的例子:
library(quantmod)
library(lubridate)
stockId<-"CVS"
showperiod<-6 # 6 months
stockData<-getSymbols(stockId, src="yahoo",auto.assign=FALSE)
startDate<-Sys.Date()-months(showperiod,abbreviate = FALSE)
fromDate<-paste0(year(startDate),month(startDate))
subset<-paste0(fromDate,"/")
mytheme <- chart_theme()
mytheme$col$dn.col <- "firebrick1"
mytheme$col$up.col <- "darkgreen"
chart_Series(stockData, name = stockId, subset = subset, theme = mytheme)
#if you add line at 2018-6-18 to 2018-07-16 & y(price)=72
#you need creat new data to plot
#
ntick<-nrow(stockData["20180618/20180716"]) #2018-6-18 to 2018-07-16 tick numbers
getDate<-index(stockData["20180618/20180716"])
y<-rep(72,ntick)
df<-data.frame(getDate,y)
linedata<-xts(df$y,order.by = df$getDate)
# add line
add_TA(linedata,on=-1,col="blue",lwd=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.