1

I have a dataframe like this one:

value = runif(n = 1000) 
type = rep(c("a","b","c","d"),250) 
type2 = rep(c("a","b"),500) 
number = sample(1:4, 1000, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
feature = c(rep("small",500),rep("big",500)) 
allResults <- data.frame(value,type,type2,number,feature)

I'd like to color the background of boxplot by type2 value. If i use fill and col, it's not very clear. I think is more intutitive the background color if is possible.

library("ggplot2")
ggplot(allResults, aes(y=value, x=type)) + geom_boxplot(alpha=.3, aes(fill = type,col=type2)) +
  ggtitle("comparison") + facet_grid(feature ~ number) +
  theme(legend.position = "bottom",axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_y_continuous(breaks =  seq(0, 1, by = 0.05),limits = c(0,1))

This is my result at the moment:

enter image description here I have seen that is possible to color the backgroud using geom_rect() but I don't understand how to apply.

TheAvenger
  • 458
  • 1
  • 6
  • 19
  • 1
    You just want to fill the background of each boxplot by `type2`? – JdeMello Nov 21 '18 at 16:52
  • yes, I want to color the background panel color like this one: https://i.stack.imgur.com/fqyah.jpg – TheAvenger Nov 21 '18 at 16:55
  • wouldn't this make more sense (so the background color does not overlap with `type`): `ggplot(allResults, aes(y=value, x=type)) + geom_boxplot(alpha=.3, aes(fill = type2, col=type2)) + ggtitle("comparison") + facet_grid(feature ~ number) + theme(legend.position = bottom",axis.text.x = element_text(angle = 90, hjust = 1)) + scale_y_continuous(breaks = seq(0, 1, by = 0.05),limits = c(0,1))` – JdeMello Nov 21 '18 at 16:58
  • Why? If I can fill why I can't use the background color? this is the right example: https://i.stack.imgur.com/CpM0p.gif – TheAvenger Nov 21 '18 at 17:01
  • 1
    try this https://stackoverflow.com/questions/50339909/shade-background-of-a-ggplot-chart-using-geom-rect-with-categorical-variables – atsyplenkov Nov 21 '18 at 17:08
  • @atsyplenkov: that example is not actually the same situation. I'd say this one is more accurate: [https://stackoverflow.com/questions/31592484/adding-shading-alternate-areas-for-categorical-variable-in-a-bar-plot-in-ggplot2](https://stackoverflow.com/questions/31592484/adding-shading-alternate-areas-for-categorical-variable-in-a-bar-plot-in-ggplot2). – JdeMello Nov 21 '18 at 17:33

1 Answers1

3

You could use geom_rect and set your divisions. I originally had a and b as your rects factors, but to match colors in your type fill just set them to a and c.

value = runif(n = 1000) 
type = rep(c("a","b","c","d"),250) 
type2 = rep(c("a","b"),500) 
number = sample(1:4, 1000, replace=TRUE, prob=c(0.25, 0.25, 0.25, 0.25) )
feature = c(rep("small",500),rep("big",500))
nFac <- 4 # define number of factors (types) here
rects <- data.frame(xmin = head(seq <- seq(0.5, nFac + .5, 1), -1), 
                  xmax = tail(seq, -1), rect_type = c("a", "c")) #set your divisions here

allResults <- data.frame(value,type,type2,number,feature, rects)

ggplot(allResults, aes(y=value, x=type)) + geom_boxplot(aes(fill = type, col=type2)) +
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = -Inf, ymax = Inf, fill = rect_type), alpha = 0.009) +
  ggtitle("comparison") + facet_grid(feature ~ number) +
  theme(legend.position = "bottom",axis.text.x = element_text(angle = 90, hjust = 1)) +
  scale_y_continuous(breaks =  seq(0, 1, by = 0.05),limits = c(0,1))

enter image description here

Anonymous coward
  • 2,061
  • 1
  • 16
  • 29