A typical way to visualize microbiome composition is by using a composition barplot which shows the relative abundances of selected taxonomic features. In the following code chunk, relative abundance is calculated, and the data is agglomerated to the phylum rank. Phyla that are not prevalent are combined into a single group called “Other” (see Chapter 9). Then, the barplot is visualized ordering rank by abundance values and samples by “Bacteroidetes” (Figure 11.1):
library(miaViz)data("GlobalPatterns")tse<-GlobalPatterns# Computing relative abundancetse<-transformAssay(tse, assay.type ="counts", method ="relabundance")# Agglomerate to phyla and combine the non-prevalent phyla into "Other"tse_sub<-agglomerateByRank(tse, rank ="Phylum")tse_sub<-addPrevalence(tse_sub, assay.type ="relabundance", detection =0.01)tse_sub<-subsetBy(tse_sub, rows =prevalence>0.2, collapse =TRUE)# Visualizing the composition barplot, with samples ordered by "Bacteroidetes"plotAbundance(tse_sub, assay.type ="relabundance", order.row.by ="abund", order.col.by ="Bacteroidetes")
Figure 11.1: Relative abundances of the prevalent phyla in the GlobalPatterns samples, with the remaining phyla grouped as Other. Phyla are ordered by abundance and samples by the relative abundance of Bacteroidetes.
Composition barplots often show more groups than color-blind-friendly palettes have colors (Section 2.4). The microshades package (Dahl et al. 2022) addresses this with palettes in which taxa of the same higher rank share shades of one color, e.g., genera within a phylum.
Microshades: An r package for improving color accessibility and organization of microbiome data. Dahl et al. (2022). Microbiol Resour Announc 11:e00795–22. doi:10.1128/mra.00795-22.
11.2 Composition heatmap
Community composition can be visualized with a heatmap, where the horizontal axis represents samples and the vertical axis the taxonomic features. The color of each intersection point represents abundance of a taxon in a specific sample.
Here, abundances are first transformed with CLR (centered log-ratio) (Aitchison 1986) to remove compositionality bias. Then standardization transformation is applied to the CLR-transformed data. This shifts all taxonomic features to zero mean and unit variance, allowing visual comparison between taxonomic features that have different absolute abundance levels. For more information on transformation methods, refer to Chapter 10.
The statistical analysis of compositional data. Aitchison (1986). Chapman & Hall.
tse<-GlobalPatterns# Agglomerate to phylum leveltse<-agglomerateByRank(tse, rank ="Phylum")tse<-addPrevalence(tse)tse<-subsetBy(tse, rows =prevalence>0.5, collapse =TRUE)# Add clr-transformation on samplestse<-transformAssay(tse, assay.type ="counts", method ="relabundance", pseudocount =1)tse<-transformAssay(tse, assay.type ="relabundance", method ="clr")# Add scale features (taxa)tse<-transformAssay(tse, assay.type ="clr", MARGIN ="rows", method ="standardize", name ="clr_z")
We can visualize the heatmap with the plotHeatmap() function from the miaViz package. With sort.method = "Heatmap", the rows and columns are ordered with hierarchical clustering, so that similar taxa and similar samples are placed next to each other. The resulting heatmap is shown in Figure 11.2.
Figure 11.2: Heatmap of the phyla in the GlobalPatterns data (rows) across samples (columns), both ordered by hierarchical clustering. Colors show CLR-transformed abundances standardized per phylum.
Another method to visualize community composition is by plotting a NeatMap, which employs radial theta sorting when plotting the heatmap (Rajaram and Oono 2010). With sort.method = "PCA_angle", plotHeatmap() performs a principal component analysis (PCA), calculates the angle (theta) of each data point on the first two principal components relative to the centroid, and sorts the data points based on these angles. This approach preserves the relationships between data points according to the ordination, rather than relying on hierarchical clustering. The order itself can be obtained with the getHeatmapOrder() function, which plotHeatmap() uses internally.
NeatMap - non-clustering heat map alternatives in R. Rajaram et al. (2010). BMC Bioinformatics 11:45. doi:10.1186/1471-2105-11-45.
Figure 11.3: NeatMap of the phyla in the GlobalPatterns data. Phyla (rows) and samples (columns) are ordered by their radial angle on the first two principal components, and colors show standardized CLR-transformed abundances.
Solve the exercises in order, as each exercise continues from the previous one. Try to solve each exercise yourself before opening the solution.
Exercise 1: Composition barplot
Goal: Visualize community composition with a relative abundance barplot.
Load the Tengeler2020 dataset from the mia package and store it in a variable called tse.
Transform the counts assay into relative abundances.
Agglomerate the data to the Phylum rank and store the result in a variable called tse_phylum.
Visualize the relative abundances of the phyla with a composition barplot. Order the phyla by abundance and the samples by the relative abundance of "Bacteroidetes".
How do you read the barplot? Why do we visualize relative abundances instead of counts?
Each bar is one sample, and the colored segments show the proportions of the phyla in that sample. Because the values are relative abundances, every bar sums up to 1. Counts depend on the sequencing depth of each sample, so bars of counts would have different heights, and the samples could not be compared directly. Ordering the samples by one phylum makes it easier to see how its proportion varies between samples and how the other phyla change along with it.
Exercise 2: Combine rare taxa into “Other”
Goal: Show the common taxa in a barplot and combine the rest into a single group.
Agglomerate tse to the Family rank so that families that are not prevalent are combined into “Other”. First agglomerate with agglomerateByRank(), then add the prevalence with addPrevalence() using the relabundance assay and a detection threshold of 0.01, and finally keep the families with a prevalence over 0.2 with subsetBy() and collapse = TRUE. Store the result in a variable called tse_family.
Check the row names of tse_family. Which families are shown separately?
Visualize the composition of tse_family with a barplot. Order the families by abundance and the samples by the relative abundance of "Lachnospiraceae".
Why is it useful to combine the less common taxa into “Other” before drawing a composition barplot?
# 1. Agglomerate to families and combine the non-prevalent ones into "Other"tse_family<-agglomerateByRank(tse, rank ="Family")tse_family<-addPrevalence(tse_family, assay.type ="relabundance", detection =0.01)tse_family<-subsetBy(tse_family, rows =prevalence>0.2, collapse =TRUE)# 2. Families in the datarownames(tse_family)## [1] "Bacteroidaceae" "Enterobacteriaceae" "Erysipelotrichaceae"## [4] "Eubacteriaceae" "Lachnospiraceae" "Porphyromonadaceae" ## [7] "Prevotellaceae" "Rikenellaceae" "Ruminococcaceae" ## [10] "Verrucomicrobiaceae" "Other"# 3. Composition barplotplotAbundance(tse_family, assay.type ="relabundance", order.row.by ="abund", order.col.by ="Lachnospiraceae")
A barplot with many taxa has many colors that are hard to tell apart. Showing only the common taxa keeps the plot readable. Combining the rest into “Other” keeps their share in the plot, so each bar still sums up to 1.
Exercise 3: Composition heatmap
Goal: Visualize community composition with a heatmap.
Agglomerate the original Tengeler2020 data to the Family rank and store the result in tse.
Transform the counts assay into relative abundances with pseudocount = 1. Then apply the clr transformation to the relabundance assay.
Standardize the clr assay feature-wise, that is, with MARGIN = "rows". Name the new assay "clr_z".
Visualize the clr_z assay with a heatmap. Order the rows and columns with hierarchical clustering.
How do you read the heatmap? Why are the values standardized for each family before plotting?
library(miaViz)# 1. Agglomerate to Family ranktse<-agglomerateByRank(Tengeler2020, rank ="Family")# 2. Relative abundances with a pseudocount, then clrtse<-transformAssay(tse, assay.type ="counts", method ="relabundance", pseudocount =1)tse<-transformAssay(tse, assay.type ="relabundance", method ="clr")# 3. Standardize the familiestse<-transformAssay(tse, assay.type ="clr", MARGIN ="rows", method ="standardize", name ="clr_z")# 4. Heatmap ordered by hierarchical clusteringplotHeatmap(tse, assay.type ="clr_z", sort.method ="Heatmap")
Each row is a family and each column is a sample. The color shows whether the family is more (positive values) or less (negative values) abundant in that sample than on average across the samples. Families differ a lot in their overall abundance. After standardization, each family has zero mean and unit variance, so the colors show the variation of each family across the samples, and abundant families do not dominate the color scale.
Exercise 4: NeatMap
Goal: Order a heatmap with the NeatMap approach.
Visualize the clr_z assay with a heatmap as in Exercise 3, but order the rows and columns with the NeatMap approach, that is, by their angle on the first two principal components.
How does the NeatMap order the samples? How does this differ from the heatmap in Exercise 3?
The NeatMap calculates the angle of each sample around the center of the PCA plot and orders the samples by this angle. Samples that are close to each other in the ordination are therefore also close to each other in the heatmap, so gradual changes in composition can be followed from left to right. In the heatmap of Exercise 3, the samples are ordered by hierarchical clustering instead, which groups similar samples but does not follow the ordination.