Add vertical lines to quantmod::chart_Series

后端 未结 2 1080
别那么骄傲
别那么骄傲 2020-12-30 07:12

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 <-          


        
2条回答
  •  粉色の甜心
    2020-12-30 07:25

    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)
    

    enter image description here

提交回复
热议问题