在R中使用geom_rect进行时间序列着色

tok*_*ing 18 r date time-series ggplot2

我试图遮蔽时间序列图的某个部分(有点像衰退阴影 - 类似于本文底部关于excel中衰退阴影的图表).我把一点点,可能是笨拙的样本放在一起来说明.我首先创建一个时间序列,用ggplot2绘制它,然后想使用geom_rect来提供着色.但我必须在争论中弄错.

a<-rnorm(300)
a_ts<-ts(a, start=c(1910, 1), frequency=12)
a_time<-time(a_ts)
a_series<-ts.union(big=a_ts, month=a_time)
a_series_df<-as.data.frame(a_series)
ggplot(a_series)+
  geom_line(mapping=aes_string(x="month", y="big"))+
  geom_rect(
    fill="red",alpha=0.5, 
    mapping=aes_string(x="month", y="big"), 
    xmin=as.numeric(as.Date(c("1924-01-01"))),
    xmax=as.numeric(as.Date(c("1928-12-31"))),
    ymin=0,
    ymax=2
    )
Run Code Online (Sandbox Code Playgroud)

请注意,我也尝试过也没用.

geom_rect(
        fill="red",alpha=0.5, 
        mapping=aes_string(x="month", y="big"), 
        aes(
           xmin=as.numeric(as.Date(c("1924-01-01"))),
           xmax=as.numeric(as.Date(c("1928-12-31"))),
           ymin=0,
           ymax=2)
        )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

zx8*_*754 15

代码工作正常,xminxmax需要转换为十进制日期,见下文,需要lubridate包.

library("lubridate")
library("ggplot2")

ggplot(a_series_df)+
  geom_line(mapping = aes_string(x = "month", y = "big")) +
  geom_rect(
    fill = "red", alpha = 0.5, 
    mapping = aes_string(x = "month", y = "big"), 
    xmin = decimal_date(as.Date(c("1924-01-01"))),
    xmax = decimal_date(as.Date(c("1928-12-31"))),
    ymin = 0,
    ymax = 2
  )
Run Code Online (Sandbox Code Playgroud)

更清洁的版本,阴影首先绘制,因此线条颜色不会改变.

ggplot() +
  geom_rect(data = data.frame(xmin = decimal_date(as.Date(c("1924-01-01"))),
                              xmax = decimal_date(as.Date(c("1928-12-31"))),
                              ymin = -Inf,
                              ymax = Inf),
            aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            fill = "grey", alpha = 0.5) +
  geom_line(data = a_series_df,aes(month, big), colour = "blue") +
  theme_classic()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 请注意,这是有效的,因为 `aes()` 被直接传递给每个 geom,而不是传递给 `ggplot()`。如果您向“ggplot()”提供“aes(x=Date,...)”并将“geom_rect()”与任何其他几何图形一起使用,就会出现奇怪的情况... (2认同)

G. *_*eck 13

它使用起来有点容易,annotate并且还注意到rectange的边界可以如下所示指定:

ggplot(a_series_df, aes(month, big)) + 
    geom_line() +
    annotate("rect", fill = "red", alpha = 0.5, 
        xmin = 1924, xmax = 1928 + 11/12,
        ymin = -Inf, ymax = Inf) +
    xlab("time")
Run Code Online (Sandbox Code Playgroud)

这也有效:

library(zoo)

z <- read.zoo(a_series_df, index = 2)
autoplot(z) + 
    annotate("rect", fill = "red", alpha = 0.5, 
        xmin = 1924, xmax = 1928 + 11/12,
        ymin = -Inf, ymax = Inf) + 
    xlab("time") +
    ylab("big")
Run Code Online (Sandbox Code Playgroud)

任何一个给出这个:

在此输入图像描述


Col*_*vel 9

要使用,geom_rect您需要通过以下方式定义矩形坐标data.frame:

shade = data.frame(x1=c(1918,1930), x2=c(1921,1932), y1=c(-3,-3), y2=c(4,4))

#    x1   x2 y1 y2
#1 1918 1921 -3  4
#2 1930 1932 -3  4
Run Code Online (Sandbox Code Playgroud)

然后你给出ggplot你的数据和阴影data.frame:

ggplot() + 
  geom_line(aes(x=month, y=big), color='red',data=a_series_df)+
  geom_rect(data=shade, 
            mapping=aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), color='grey', alpha=0.2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述