我正在尝试绘制我的植物的生长情况。但我想用不同颜色的 geom_rect() 来突出不同的阶段。我的代码在全部放在一起时会执行此操作,但是当将它们与 facet_wrap() 分开时,我得到一个 aes() 错误,有人可以看看它并提出一些建议吗?
错误:美学必须是长度1或与数据相同(9):填写运行rlang::last_error()
以查看错误发生的位置。
代码:
Growth_Phase = data.frame(Begin_x=c(0,5,21),End_x=c(5,21,50),
Begin_y=c(-Inf, -Inf, -Inf),
End_y=c(Inf, Inf, Inf),
Period_Label=c("Dark \nGermination","Nursery","Farm"))
Plant_Height = ggplot() + theme_bw() +
facet_wrap(~Cultivar, nrow=3) +
geom_rect(data=Growth_Phase, mapping=aes(xmin=Begin_x, xmax=End_x, ymin=Begin_y, ymax=End_y),
fill=c("gray","light blue","light green"), color="black", alpha=0.3) +
stat_summary(Plant_Development, mapping=aes(x=Day, y=Height, group=Type),
fun.data=mean_se, geom = "errorbar", color="black", size=1, width = 1, na.rm = T) +
stat_summary(Plant_Development, mapping=aes(x=Day, y=Height, colour=Type),
fun="mean", geom = "line", size=1, na.rm = T) +
stat_summary(Plant_Development, mapping=aes(x=Day, y=Height, colour=Type),
fun="mean", geom = "point", size=2, shape=16, na.rm = T) +
geom_label(data=Growth_Phase, aes(x=Begin_x+(End_x-Begin_x)/2, y=18, label=Period_Label),
color="black", size=3, fontface = "bold") +
scale_x_continuous(breaks=seq(0,50,5), limits=c(0,50)) +
scale_y_continuous(breaks=seq(0,20,5), limits=c(0,20)) +
labs(title = "Average plant height", x="Day", color = "", y = "Plant Height (cm)", caption = "Cesar Figueroa Lopez") +
theme(title = element_text(size=18),
axis.title.y = element_text(size=18),
axis.text.y = element_text(size=18),
axis.title.x = element_text(size=18),
axis.text.x = element_text(size=18),
legend.text = element_text(size=18),
legend.title = element_text(size=18),
legend.position = "bottom",
strip.text = element_text(size=20))
Plant_Height