Making figures for microbial ecology: Interactive bar plots


This is the first of several tutorials I’m putting together for making figures that are common in microbial ecology. Today we’ll start with an interactive bar plot for exploring your microbial community data. For this tutorial I’m using data and code from my publication in Geobiology. You can fork the repo by clicking the button above!

To generate the data, we sequenced DNA from microbial communities at our study site DeMMO. The raw sequence data was processed with QIIME to produce an OTU table. OTUs, or operational taxonomic units, are bins that differentiate sequences at a 97% similarity threshold. For our purposes, you can think of an OTU as a species of bacteria or archaea. As a microbial ecologist, you might want to compare the taxonomic compositions of your communities. Let’s make a cool html bar plot to explore our microbial community data!

Before we get started, you’ll need to set up your environment in R. This code depends on the packages being loaded here:

#load libraries 
pacman::p_load(tidyverse, plotly, randomcoloR)

Next, read the OTU data into a dataframe. We can pull the data directy from Github by reading the raw file. You can preview the data in the table below this code chunk.

#read the data into a dataframe
otu_table <- read_delim("", delim="\t", comment = "# ")
metadata <- read_csv("") 

#store the taxonomy for each OTU
taxonomy <- otu_table %>%
  select(`#OTU ID`, taxonomy) %>%
  mutate(tax = gsub("Gammaproteobacteria; D_3__Betaproteobacteriales", "Betaproteobacteria; D_3__Betaproteobacteriales", taxonomy), #fix taxonomy for Beta's,
         taxonomy = str_remove_all(tax, "D_0__| D_1__| D_2__| D_3__| D_4__| D_5__| D_6__")) %>%
  separate(taxonomy ,sep=';',c("domain", "phylum", "class", "order", "family", "genus", "species")) %>%
  gather(level, taxonomy, domain:species)

We want to look at the community compositions, so first let’s normalize and reshape the data.

abundance_table <- otu_table %>%
  select(-taxonomy) %>%
  mutate_at(vars(-`#OTU ID`), funs(./sum(.)*100)) %>% #normalize to relative abundance 
  gather(sample_id, abundance, `7.DeMMO1.Steri.050917`:`18.800.DitchFluid.041818`) 

For plotting purposes, let’s pick a subset of samples.

barplot_samples <- c("12.DeMMO1.steri.041818", 

Now, let’s take a look at the phylum level abundances for each sample.

taxon_abundance_table <- abundance_table %>%
  left_join(taxonomy) %>%
  filter(sample_id %in% barplot_samples & level == "phylum") %>%
  mutate(taxonomy = if_else(, "Unassigned", taxonomy))

Let’s make a custom color palette for our phyla.

phylum_color <- distinctColorPalette(k = length(unique(taxon_abundance_table$taxonomy)))
names(phylum_color) <- unique(taxon_abundance_table$taxonomy)

And now, let’s create a stacked bar plot for each site.

bar_plot <- taxon_abundance_table %>%
  left_join(metadata) %>%
  group_by(site, experiment.type, taxonomy) %>%
  summarise(abundance = sum(abundance)) %>%
  ungroup() %>%
  mutate(experiment.type = factor(experiment.type, levels = c("fluid", "inert.control", "pyrolusite", "pyrite","hematite","magnetite","siderite","gypsum","muscovite","calcite"))) %>%
  ggplot(aes(fill=taxonomy, y=abundance, x=experiment.type)) +
  geom_bar(stat='identity', position='fill') +
  scale_fill_manual(values=phylum_color) +
  coord_flip() + 
  theme(axis.title = element_blank(),
        legend.title = ggplot2::element_blank(), 
        legend.text = ggplot2::element_text(size = 8),
        legend.key.size = unit(0.5, "cm")) +
  facet_grid(cols = vars(site), switch = "y") + 
  guides(fill = guide_legend(ncol = 1))

Finally, let’s make it easier to explore our data with an html version of our bar plot!


Ta da! You now have an interactive html version of your plot that you can view in your web browser - just double click the file in your file explorer and it will automatically open. These files are portable, so you can email them to your collaborators and explore the data together. Stay tuned for upcoming tutorials on making figures for microbial ecology!