how to get different background color based on a variable in box plot


I am trying to understand how I can get a different background color based on a variable in Box plot.
Currently, I am using the below ggplot2 code for the box plot but it will be great to change the background color based on a variable.

p <- ggplot(data = data, aes(x=SampleType, y=value,fill=SampleType)) +
theme_bw() +
      plot.title = element_text(vjust = -8.5,hjust = 0.1),
      axis.text.x = element_text(size = 10, face ="bold", hjust = 0.5, colour = "black"),
      axis.text.y = element_text(size = 12, face ="bold", hjust = 0.5, colour = "black"),
      axis.ticks.x = element_blank()) +

theme(strip.background=element_rect(fill="white")) +
theme(strip.text=element_text(color="black",face="bold", size =14)) + 
geom_boxplot(aes(fill=SampleType),outlier.colour = NA) 
p + facet_wrap(.~name,scales="free_y", ncol = ncol)

I have also attached the example figure that has a different background color.

Many thanks,
Fig.pdf (15.7 KB)


This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.