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.

6
  • 1
    You just want to fill the background of each boxplot by type2? Commented Nov 21, 2018 at 16:52
  • yes, I want to color the background panel color like this one: i.sstatic.net/fqyah.jpg Commented Nov 21, 2018 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)) Commented Nov 21, 2018 at 16:58
  • Why? If I can fill why I can't use the background color? this is the right example: i.sstatic.net/CpM0p.gif Commented Nov 21, 2018 at 17:01
  • 1
    try this stackoverflow.com/questions/50339909/… Commented Nov 21, 2018 at 17:08

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.