Distinguish theme (background) color for negative and positive values in geom_boxplot

Solution 1:

One option would be to add different filled backgrounds using geom_rect:

library(ggplot2)

ggplot(df, 
       aes(x = Q, y = slope, color = Recipient)) + 
  geom_rect(data = data.frame(
    xmin = c(-Inf, -Inf),
    xmax = c(Inf, Inf),
    ymin = c(-Inf, 0),
    ymax = c(0, Inf),
    fill = c("darkblue", "lightblue")
  ), aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = fill), inherit.aes = FALSE, alpha = .5) +
  scale_fill_manual(values = c("darkblue" = "darkblue", "lightblue" = "lightblue"), guide = "none") +
  geom_boxplot(notch = TRUE)