I am working with some time series data and would like to highlight chart area whenever certain conditions become true. For example:
require(ggplot2)
require(quantmod)
initDate <- "1993-01-31"
endDate <- "2012-08-10"
symbols <- c("SPY")
getSymbols(symbols, from=initDate, to=endDate, index.class=c("POSIXt","POSIXct"))
spy<-SPY$SPY.Adjusted
spy$sma<-SMA(spy$SPY.Adjusted,200)
spy<-spy[-(1:199),]
spy<-as.data.frame(spy)
ggplot(spy,aes(x=index(spy),y=spy$SPY.Adjusted))+geom_line()+geom_line(aes(x=index(spy),y=spy$sma))
The above code plots the the data, but how can I highlight the section when ever close is above sma? This question is similar to How to highlight time ranges on a plot?, but then it is manual. Is there a function in ggplot2 for conditional plotting?
See Question&Answers more detail:os