简体   繁体   English

如何在ggplot2图中突出显示时间序列项

[英]How to highlight an item of time-series in a ggplot2 plot

I wish to highlight segments above or below a certain value in a time series by a unique colour or a shape. 我希望通过唯一的颜色或形状突出显示时间序列中某个值以上或以下的分段。 In the example data I am decomposing a mortality time series into its components. 在示例数据中,我将死亡率时间序列分解为其组成部分。 My goal is to highlight the segments when the mortality in the trend component falls below 35 (deep between 1997 and 2000) and when the residual component is above 100 (the spike). 我的目标是突出显示趋势部分的死亡率低于35(1997年至2000年之间的深度)以及剩余部分高于100(峰值)的细分。 I have tried to use annotate, but that did not produce what I wanted. 我尝试使用注释,但是没有产生我想要的。

#Load library and obtain data

library(gamair) 
library(tsModel)
library(ggplot2)
library(reshape2)
data<-data(chicago)

## create variables, decompose TS
chicago$date<-seq(from=as.Date("1987-01-01"), to=as.Date("2000-12-31"),length=5114)
data<- chicago[,c("date","death")]
mort <- tsdecomp(data$death, c(1, 2, 15, 5114))

## Convert matrix to df, rename, melt
df<-as.data.frame(mort)
names(df)[1] <- "Trend"
names(df)[2] <- "Seasonal"
names(df)[3] <- "Residual"
df$date<-seq(as.Date("1987-01-01"), as.Date("2000-12-31"), "day")
meltdf <- melt(df,id="date")

##  Plot 

ggplot(meltdf,aes(x=date,y=value,colour=variable,group=variable)) + geom_line() +
theme_bw() +
ylab("") + xlab("") + 
facet_grid(variable ~ . , scales = "free") +
theme(legend.position = "none") 
annotate("rect", xmin=1995-01-01,xmax=1996-01-01,ymin= 10, ymax=300, alpha = .2,fill="blue")

在此处输入图片说明

Well, this works but I must admit it's more work that I'd hoped. 好吧,这行得通,但我必须承认,这是我希望做的更多工作。

get.box <- function(data) {
  rng <- range(data$date) + c(-50,50)
  z   <- meltdf[meltdf$date>=rng[1] & meltdf$date <=rng[2] & meltdf$variable==unique(data$variable),]
  data.frame(variable=unique(z$variable),
             xmin=min(z$date),xmax=max(z$date),ymin=min(z$value),ymax=max(z$value))
}
hilight.trend <- get.box(with(meltdf,meltdf[variable=="Trend" & value<35,]))
hilight.resid <- get.box(with(meltdf,meltdf[variable=="Residual" & value>100,]))
ggplot(meltdf,aes(colour=variable,group=variable)) + 
  geom_line(aes(x=date,y=value)) +
  theme_bw() +
  ylab("") + xlab("") + 
  facet_grid(variable ~ . , scales = "free") +
  theme(legend.position = "none") +
  geom_rect(data=hilight.trend, alpha=0.2, fill="red",
            aes(xmax=xmax,xmin=xmin,ymax=ymax,ymin=ymin)) +
  geom_rect(data=hilight.resid, alpha=0.2, fill="blue", 
            aes(xmax=xmax,xmin=xmin,ymax=ymax,ymin=ymin))

You can't really use annotate(...) with facets, because you will get the same annotation on all the facets. 您实际上不能对构面使用annotate(...) ,因为您将在所有构面上获得相同的注释。 So you're left with something like geom_rect(...) . 因此,您剩下的东西像geom_rect(...) The problem here is that geom_rect(...) draws a rectangle for every row in the data . 这里的问题是geom_rect(...) 为data中的每一行绘制一个矩形。 So you need to create an auxiliary dataset with just one row for each variable , containing the x- and y- min and max. 因此,您需要为每个variable创建一个仅包含一行的辅助数据集,其中包含x和y-最小值和最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM