I am trying to annotate each facet of my ggplot with a different geom_rect object. Reproducible example:
a <- c(x= rnorm(30, mean = 100, sd=2))
b <- c(rep(c("X", "Y"), 30))
c <- c(rep("F", 15), rep("M",15))
abc <- data.frame(a, b, c)
This is my ggplot:
ggplot(abc, aes(x = b , y = a, fill=b)) +
geom_boxplot(alpha= 0.8) +
geom_point(position = position_dodge(width=0.75))+
ylab(label = "a")+
scale_fill_manual(values = c("blue", "red"))+
stat_compare_means(label.x.npc = "center")+
facet_wrap(~c)
I've used the following code for ggplots before.
annotate("rect",
xmin = -Inf, xmax = Inf,
ymin = 90, ymax = 100,
fill= "grey", alpha= 0.4)
But now I want to annotate each facet F and M separately. For F, I want to annotate y spanning from 90 to 100, For M, I want to annotate y spanning from 100 to 110, x will -inf to inf for both
I read about using geom_rect and some dummy df for this but cannot understand how to use it for such a case. Any help is greatly appreciated!