joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
586 stars 186 forks source link

customize facet colors in ggplot/ phyloseq object #1714

Open science-chump opened 12 months ago

science-chump commented 12 months ago

Hello, I am trying to customize the background colors of the facets in my stacked bar plot and am running into issues.

Here is what the plot looks like: image

The problem is that the only color that appears is the first one in the sequence, and not the rest. This is the code I am using to plot it:

 site_colors <- c("#35756B","#35756B","#35756B","#FFBF29","#A6611A","#4B0082","#4B0082")
 Genus_plot_final2 <- plot_bar(physeq_bet, x = "Sample_ID", fill = "Genus") + facet_grid(~Site, scales = "free_x", space = "free_x", labeller = as_labeller(samples))
Genus_plot_final2 <- Genus_plot_final2 + labs(x = "Sample_ID", y = "Relative Abundance") + theme(axis.text.x = element_text(angle = 45, hjust = 1))
Genus_plot_final2 <- Genus_plot_final2 + theme(strip.background = element_rect(fill = c(site_colors))) 

Is there a way that I can get each facet strip to be its own unique color?

adossantos1 commented 11 months ago

Not an easy way but there are ways: https://stackoverflow.com/questions/53455092/r-ggplot2-change-colour-of-font-and-background-in-facet-strip https://stackoverflow.com/questions/70841252/how-to-specify-different-background-colors-for-each-facet-label-in-ggplot2

Easy way with an extra package ggh4x https://stackoverflow.com/questions/19440069/ggplot2-facet-wrap-strip-color-based-on-variable-in-data-set

element_rect() only takes one value for fill sadly