Giorgio Garziano
2016-Apr-20 14:45 UTC
[R] Add a vertical arrow to a time series graph using ggplot and xts
Please see updates to df2 assignment as shown below. library(xts) # primary #library(tseries) # Unit root tests library(ggplot2) library(vars) library(grid) dt_xts<-xts(x = 1:10, order.by = seq(as.Date("2016-01-01"), as.Date("2016-01-10"), by = "1 day")) colnames(dt_xts)<-"gdp" xmin<-min(index(dt_xts)) xmax<-max(index(dt_xts)) df1<-data.frame(x = index(dt_xts), coredata(dt_xts)) p<-ggplot(data = df1, mapping= aes(x=x, y=gdp))+geom_line() rg<-ggplot_build(p)$panel$ranges[[1]]$y.range y1<-rg[1] y2<-rg[2] # x = as.Date(..) in place of x = "2016-01-05" df2<-data.frame(x = as.Date("2016-01-05"), y1=y1, y2=y2 ) p1<-p+geom_segment(mapping=aes(x=x, y=y1, xend=x, yend=y2), data=df2, arrow=arrow()) -- Best, GG [[alternative HTML version deleted]]