I am trying to plot the growth of my plants. But I want to highlight the different stages with a geom_rect() of different colors. My code does it when it is all together, but when dividing them with facet_wrap() I get an aes() error, can somebody have a look at it and suggest something?
Error: Aesthetics must be either length 1 or the same as the data (9): fill
Run rlang::last_error()
to see where the error occurred.
Code Output -> Looking for this
Code:
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