plot_nested_bar: Plot a nested bar plot

View source: R/plot_nested_bar.R

plot_nested_barR Documentation

Plot a nested bar plot

Description

This function takes a phyloseq object and plots the abundance of taxa in each sample at two levels: a top level (e.g. Phylum), using colours, and a nested level (e.g. Species), using shades and tints of each colour. It is intended to be used together with top_taxa or nested_top_taxa, but functions with any phyloseq object. The output is a ggplot object, and can be manipulated as such, although take note of the documentation for ggnested.

Usage

plot_nested_bar(
  ps_obj,
  top_level,
  nested_level,
  top_merged_label = "Other",
  nested_merged_label = "Other <tax>",
  palette = NULL,
  base_clr = "#008CF0",
  merged_clr = "grey90",
  include_rank = T,
  na_taxon_label = "<tax> (<rank>)",
  asv_as_id = F,
  duplicate_taxon_label = "<tax> <id>",
  relative_abundances = T,
  sample_order = NULL,
  ...
)

Arguments

ps_obj

A phyloseq object, ideally generated by top_taxa or nested_top_taxa

top_level

The taxonomic level with which to fill the bars

nested_level

The taxonomic level at which to generate shades and tints

top_merged_label

The (top_)merged_label that was used in top_taxa or nested_top_taxa.

nested_merged_label

The nested_merged_label that was used in nested_top_taxa.

palette

A custom palette. See taxon_colours.

base_clr

A base colour for palette generation. See taxon_colours.

merged_clr

A colour for the merged taxon. See taxon_colours.

include_rank

Include rank when naming NA taxa? See name_na_taxa.

na_taxon_label

Label format for NA taxa. See name_na_taxa.

asv_as_id

Use ASVs as IDs for duplicate taxa? See label_duplicate_taxa.

duplicate_taxon_label

Label format for duplicate taxa. See label_duplicate_taxa.

relative_abundances

Plot relative abundances?

sample_order

Character vector with all sample names in order.

...

options to be passed to ggnested

Details

This function is a wrapper around ggnested, with some accessory functions to ensure the plot comes out neatly It runs through the following steps:

  • Generate a palette using taxon_colours

  • Name NA taxa using name_na_taxa

  • Label identical taxa using label_duplicate_taxa

  • Convert phyloseq to a data frame using psmelt

  • Refactor merged taxa using move_label and move_nested_labels

  • Generate a nested barplot using ggnested

See the documentation for each function for details.

Value

A ggplot2 object

Examples


# Plot the Phylum and Species of the 20 most abundant ASVs
ps_obj <- GlobalPatterns
top <- top_taxa(ps_obj, n_taxa = 20,
                FUN = median)
p <- plot_nested_bar(top$ps_obj,
                     "Phylum",
                     "Species")
p

# Order the samples by the abundance of Other and plot with faceting
sample_order <- p$data %>%
    group_by(Sample) %>%
    mutate(Abundance = Abundance / sum(Abundance)) %>%
    filter(Phylum == "Other") %>%
    arrange(Abundance) %>%
    pull(Sample)
plot_nested_bar(top$ps_obj,
                "Phylum",
                "Species",
                sample_order = sample_order) +
  facet_wrap(~SampleType, scales = "free_x")

# Plot the nested_top_taxa
top_level <- "Phylum"
nested_level <- "Species"
top_merged_label <- "Other"
nested_merged_label <- "Other"
top <- nested_top_taxa(ps_obj,
                       top_tax_level = top_level,
                       nested_tax_level = nested_level,
                       n_top_taxa = 3,
                       n_nested_taxa = 3)
plot_nested_bar(top$ps_obj, top_level, nested_level)

# Order by SampleType and alter the colours for some taxa
sample_order <- sample_data(top$ps_obj) %>%
data.frame() %>%
  arrange(SampleType, X.SampleID) %>%
  pull(X.SampleID) %>%
  as.character()
plot_nested_bar(top$ps_obj,
                top_level = top_level,
                nested_level = "Genus",
                palette = c(Bacteroidetes = "red",
                           Proteobacteria = "blue"),
                sample_order = sample_order)


gmteunisse/Fantaxtic documentation built on June 7, 2024, 8:47 a.m.