rggplot2phyloseq

Trying to change colour of one variable in ggplot geom_bar dependent on the string


I have a for loop to run through a tonne of microbiome data (using phyloseq) and generate plots for multiple experiments.

  ggplot(data_M1, aes(x = Sample, y = Abundance, fill = get(i))) +
    geom_bar(stat = "identity")+
    facet_wrap(vars(Status, Time.Point, Treatment), scales = "free", ncol=2)+
    theme(axis.title.x=element_blank(),
          axis.text.x=element_blank(),
          axis.ticks.x=element_blank())+
    guides(fill = guide_legend(reverse = TRUE, keywidth = 1, keyheight = 1, title = i))+
    ylab(yaxisname)+
    ggtitle(plotname)+
  ggsave(ggsavename, last_plot())

Example outcome: example of figure produced

What I am trying to do though is make all the "_unclassified" samples/ sequencing data grey... so maybe I need some kind of if statement with str_contains?

Happy to dput a reproducible example if required but someone might have a simple solution.

Thank you!


Solution

  • @camille's comment about a minimal reproducible example is germaine. We need know nothing about your facets, guides or call to ggsave to answer your question.

    First, generate some test data

    library(tidyverse)
    
    d <- tibble(
           Species=rep(c("s__reuteri", "s__guilliermondii", 
                         "o__Clostridiales_unclassified", "k__bacteria_unclassified"), 
                       each=4),
           Sample=as.factor(rep(1:4, times=4)),
           Abundance=runif(16)
         )
    

    Generate custom labels and colours

    labels <- unique(d$Species)
    # Make sure length of availableColours is long enough to accommodate the maximum length of labels
    availableColours <- c("red", "blue", "green", "orange", "yellow")
    legendColours <- ifelse(str_detect(labels, fixed("unclassified")), "grey", availableColours)
    

    Create the plot

    d %>% 
      ggplot(aes(x=Sample, y=Abundance, fill=Species)) +
      geom_bar(stat="identity") +
      scale_fill_manual(labels=labels, values=legendColours)
    

    Giving

    enter image description here

    If you want to "pool" all the unclassified species, then

    d1 <- d %>% 
            mutate(
              LegendSpecies=ifelse(
                              str_detect(
                                Species, 
                                fixed("unclassified")
                              ), 
                              "Unclassified", 
                              Species
                            )
            )
    
    legendColours <- ifelse(str_detect(unique(d1$LegendSpecies), fixed("Unclassified")), "grey", availableColours)
    
    d1 %>% 
      ggplot(aes(x=Sample, y=Abundance, fill=LegendSpecies)) +
      geom_bar(stat="identity")+
      scale_fill_manual(labels=unique(d1$LegendSpecies), values=legendColours)
    
    

    Giving

    enter image description here