向quantmod::chart\u系列添加垂直线
我想在某个图表的几个日期上添加垂直线。到目前为止,我还没有完成这个简单的任务。这就是我所尝试的:向quantmod::chart\u系列添加垂直线,r,quantmod,R,Quantmod,我想在某个图表的几个日期上添加垂直线。到目前为止,我还没有完成这个简单的任务。这就是我所尝试的: > 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 gra
> 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
>s d1
[1] "2012-05-24"
>图表系列(s,TA=“添加行(v=d1)”)
get.current.chob()中出错:设置不正确或缺少图形设备
>图表(s)
>abline(v=d1)
#没什么
>添加(“添加行(v=d1”)
“[.data.frame”中出错(lenv$xdata,Env$xsubset):
选择未定义的列
根据我在这里已经读到的内容,我知道abline
不应该与新的chart_Series
函数一起工作。它似乎无论如何都不工作。addLines
函数在我尝试过的任何形式下都不工作-普通addLines
,plot(addLines(…)
,chart_Series(…),TA=“添加行(…)”
或添加(“添加行(…)”)
我需要使用quantmod的实验版本,因为它解决了我在旧版本中遇到的其他问题。
d1
最终将是一个日期列表。你不能混合使用quantmod图表函数的新旧版本。如果你想使用添加行
,你必须使用图表系列
即使使用addLines
和chartSeries
,d1
也应该是xts对象,而不是datetime对象。例如:
library(quantmod)
data(sample_matrix)
s <- as.xts(sample_matrix)
chartSeries(s,TA="addLines(v=s[100])")
l <- xts(!as.logical(s[,1]),index(s))
l[100] <- TRUE
chart_Series(s,TA="add_TA(l,on=1)")
还请注意,您可以在add_TA
调用中使用on=-1
将垂直线放在图表的“后面”:
chart_Series(s,TA="add_TA(l,on=-1,col='grey',border='grey')")
在我的示例中添加水平线:
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)
库(quantmod)
图书馆(lubridate)
StockId但你在这里添加了一条垂直线而不是水平线,不是吗?@agstudy:是的,我认为这是OP标题中的一个输入错误。我不知道如何在特定日期添加一条水平线,因为日期在x轴上。好的。谢谢+1。我明白逻辑,我们只叠加时间序列(这里是xts对象),否则这就没有“意义”“@agstudy:没错;这是一个简单的假设,因为chart\u Series
只适用于时间序列对象。谢谢Joshua Ulrich,这正是我想要的。