我想基于特定日期(在X轴上)更改散点图中的背景颜色。 Ma的日期范围为2017年6月23日至2017年12月6日。我希望背景为6月23日至8月31日为绿色,其余为红色。
我在Change background color panel based on year in ggplot R处尝试过使用此脚本,但是它不起作用(老实说,我从未使用过ggplot2)。 日期变量是POSIXct格式。这是我使用的脚本,带有R给出的错误:
> ggplot() + geom_point() +
geom_rect(aes(xmin = as.Date("2017-06-23"),xmax = as.Date("2017-08-31"),ymin = 0,ymax = Inf),fill="green",alpha = .2)+
geom_rect(aes(xmin = as.Date("2017-09-01"),xmax = as.Date("2017-12-06"),fill="red",alpha = .2)
Errore: Invalid input: time_trans works with objects of class POSIXct only
此脚本有什么问题(或遗漏了)?
如果有用的话,这是我的数据集str()
的{{1}}
data
如果要尝试,这里有一些数据(数据集的前30行):
str(data)
'data.frame': 420 obs. of 2 variables:
$ UTC.Date : POSIXct,format: "2017-07-01" "2017-08-01" "2017-09-01" "2017-10-01" ...
$ Mean.elevation : num 1353 1098 905 747 1082 ...