Search code examples
rggplot2boxplotphyloseq

Coloring by variable for phyloseq boxplots


I have a boxplot (see below) that I'd like to color by the variables according to a specified palette. I'm not sure how to color them as the code is based on the Phyloseq package. I tried having aes() in plot_richness, but it seems to only work for geom_boxplot().

library(ggplot2)
library(phyloseq)

palette <- c("#B0F2E7", "#166AD0", "#F89EE9", "#DA0000", "#C6C3D3", "#23202C")

plot_richness(physeq_shime, x='System', measures='Shannon') + theme_bw() + 
  xlab('SHIME') + scale_y_continuous(limits=c(2.0,3.2)) + 
  geom_boxplot(aes(fill='System')) 

How can I fill the boxplots in the order of the custom palette?

Thanks in advance.

Figure 1. Boxplots in Phyloseq


Solution

  • This worked for me

    library(ggplot2)
    library(phyloseq)
    
    palette <- c("#B0F2E7", "#166AD0", "#F89EE9", "#DA0000", "#C6C3D3", "#23202C")
    
    plot_richness(physeq_shime, x='System', measures='Shannon') + theme_bw() + 
      xlab('SHIME') + scale_y_continuous(limits=c(2.0,3.2)) + 
      geom_boxplot(lwd=0.9, alpha=0.7, aes(fill=sample_data(physeq_shime)$System)) +
      scale_fill_manual(values=palette)