ggplot2:突出显示图表区域 [英] ggplot2: highlight chart area

查看:171
本文介绍了ggplot2:突出显示图表区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些时间序列数据,并希望在某些条件成立时突出显示图表区域。例如:

  require(ggplot2)
require(quantmod)
initDate< - 1993- 01-31
endDate< - 2012-08-10
symbols< - c(SPY)
getSymbols(符号,从= initDate到= 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。已调整))+ geom_line()+ geom_line(aes(x = index(spy),y = spy $ sma))

上面的代码绘制了这些数据,但是我怎样才能突出显示这个部分,当关闭超过sma时?这个问题类似于由Michael Weylandt创建,作为Google夏季代码 project。

  spy < -  as.xts(spy)
require(xtsExtra)
plot(spy,screens = 1,
blocks = list(start.time = paste(index (spy)[l $ start]),
end.time = paste(index(spy)[l $ end]),col ='lightblue'),
legend.loc ='bottomright', auto.legend = TRUE)


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?

解决方案

Based on code in the TA.R file of the quantmod package, here is code that uses rle to find the starts and ends of the rectangles.

runs <- rle(as.logical(spy[, 1] > spy[, 2]))
l <- list(start=cumsum(runs$length)[which(runs$values)] - runs$length[which(runs$values)] + 1,
          end=cumsum(runs$lengths)[which(runs$values)])
rect <- data.frame(xmin=l$start, xmax=l$end, ymin=-Inf, ymax=Inf)

Combine that with some ggplot2 code from the accepted answer to the question you linked to:

ggplot(spy,aes(x=index(spy),y=spy$SPY.Adjusted))+geom_line()+geom_line(aes(x=index(spy),y=spy$sma))+geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), color="grey20", alpha=0.5, inherit.aes = FALSE)

And you get:

If you reverse the order of plotting and use alpha=1 in geom_rect it may (or may not) look more like you desire:

ggplot(spy,aes(x=index(spy),y=spy$SPY.Adjusted))+geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax), border=NA, color="grey20", alpha=1, inherit.aes = FALSE)+geom_line()+geom_line(aes(x=index(spy),y=spy$sma))


Since you have an xts object. You may not even want to convert to a data.frame. Here is how you could plot it using the brand new plot.xts method in the xtsExtra package created by Michael Weylandt as part of a Google Summer of Code project.

spy <- as.xts(spy)
require(xtsExtra)
plot(spy, screens=1,
     blocks=list(start.time=paste(index(spy)[l$start]),
                 end.time=paste(index(spy)[l$end]), col='lightblue'),                    
     legend.loc='bottomright', auto.legend=TRUE)

这篇关于ggplot2:突出显示图表区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆