我正在尝试为时间序列图中的某个部分进行阴影处理(有点像经济衰退的阴影处理 - 类似于此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
)
请注意,我也尝试了 which 命令,但它也没有起作用。
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)
)
annotate("rect",...)
函数是否接受边框参数?我找不到相关文档。我想要得到一个只有边框的红色矩形,没有填充。 - Urvah Shabbircol=
而不是fill=
。 - G. Grothendieckannotate("segment",...)
。还是谢谢,我会在将来尝试使用它。 - Urvah Shabbirgeom_errorbar()
)。 - Rich Pauloolibrary(ggplot2) ggplot(a_series_df, aes(month, big)) + geom_line() + annotate("rect", fill = "red", alpha = 0.5, xmin = c(1915, 1924), xmax = c(1920, 1928) + 11/12, ymin = -Inf, ymax = Inf) + xlab("time")
- G. Grothendieck