I want to make a boxplot with a horizontal rectangle behind the plots (or above with opacity). I am not sure how to do this with categorical x-axis values and force it to extend the entire width of the plot area. This answer was helpful, but I'm not sure how to extend the rectangle to the edges of the plot area.
Let's say we want to make a rectangle across the diamonds
cuts between $3000 and $5000. I have attempted the code below, but it doesn't extend to the edge of the plot.
rectangle <- data.frame(x = c("Fair","Good","Very good", "Premium", "Ideal"),
lower = rep(3000, 5),
upper = rep(5000, 5))
ggplot() +
geom_boxplot(data=diamonds, aes(x=cut, y=price)) +
geom_rect(data=rectangle, aes(xmin="Fair", xmax="Ideal",
ymin=lower, ymax=upper), alpha=0.1)
Output