Search code examples
rggplot2pie-chart

Conditionally change background colour facet_grid with pie charts


I am using ggplot2 with facet grid to make a graph of pie charts that have different sizes depending on the sample size. I would like the background of the pie charts have a different colour depending on again a different variable (yes I know this is a lot of information in one graph, but in the end it will give the best overview of our results). However if I use the answer given here my pie charts disappear and the colours get disrupted between geom_rect and geom_bar. Is there a way to change the background colour without fiddling with the colours of the pie charts?

My current code is as follow:

ggplot(test,aes(x=No._plants_EW/2,y= No._HR,fill=HR,width=No._plants_EW)) + 
 geom_bar(stat="identity",position="fill") + facet_grid(phylo ~ LineNumber) + 
 coord_polar("y") + 
 scale_fill_manual(values=c("HRMin"="grey54", "HRPlus"="#E69F00")) + 
 blank_theme + 
 theme(axis.text.x=element_blank(),axis.text.y=element_blank()) + 
 theme(strip.text.y = element_text(angle = 0, hjust=0.5))

Thank you very much for all your help!


Solution

  • You are not able to do this in R, especially when using polar coordinates, from what I have read.

    I was trying this exact thing for a pie graph similar to the one you describe. The issue is what @MKBakker points out. Backgrounds are a non-data element and the solution from the link you posted won't work because you have polar coordinate and there is no way to plot your data in the corners to cover the background color.

    I ended up just manually highlighting the facets after adding my plot to my document.