plotAbundance: Plotting abundance data

plotAbundanceR Documentation

Plotting abundance data

Description

plotAbundance plots the abundance on a selected taxonomic rank. Since this probably makes sense only for relative abundance data, the assay used by default is expected to be in the slot ‘relabundance’. If only ‘counts’ is present, the relative abundance is computed.

Usage

plotAbundance(x, ...)

## S4 method for signature 'SummarizedExperiment'
plotAbundance(
  x,
  rank = NULL,
  col.var = features,
  features = NULL,
  order.row.by = order_rank_by,
  order_rank_by = c("name", "abund", "revabund"),
  order.col.by = order_sample_by,
  order_sample_by = NULL,
  decreasing = TRUE,
  layout = c("bar", "point"),
  one.facet = one_facet,
  one_facet = TRUE,
  ncol = 2,
  scales = "fixed",
  assay.type = assay_name,
  assay_name = "counts",
  ...
)

Arguments

x

a SummarizedExperiment object.

...

additional parameters for plotting.

  • as.relative Character scalar. Should the relative values be calculated? (Default: FALSE)

See mia-plot-args for more details i.e. call help("mia-plot-args")

rank

Character scalar. Defines the taxonomic rank to use. Must be a value of taxonomyRanks(x). (Default: NULL)

col.var

Character scalar. Selects a column from colData to be plotted below the abundance plot. Continuous numeric values will be plotted as point, whereas factors and character will be plotted as colour-code bar. (Default: NULL)

features

Deprecated. Use col.var instead.

order.row.by

Character scalar. How to order abundance value: By name (“name”) for sorting the taxonomic labels alphabetically, by abundance (“abund”) to sort by abundance values or by a reverse order of abundance values (“revabund”).

order_rank_by

Deprecated. Use order.row.by instead.

order.col.by

Character scalar. from the chosen rank of abundance data or from colData to select values to order the abundance plot by. (Default: NULL)

order_sample_by

Deprecated. Use order.col.by instead.

decreasing

Logical scalar. If the order.col.by is defined and the values are numeric, should the values used to order in decreasing or increasing fashion? (Default: FALSE)

layout

Character scalar. Either “bar” or “point”.

one.facet

Logical scalar. Should the plot be returned in on facet or split into different facet, one facet per different value detect in rank. If col.var or order.col.by is not NULL, this setting will be disregarded. (Default: TRUE)

one_facet

Deprecated. Use one.facet instead.

ncol

Numeric scalar. if one.facet = FALSE, ncol defines many columns should be for plotting the different facets. (Default: 2)

scales

Character scalar. Defines the behavior of the scales of each facet. Both values are passed onto facet_wrap. (Default: "fixed")

assay.type

Character scalar value defining which assay data to use. (Default: "relabundance")

assay_name

Deprecate. Use assay.type instead.

Details

Subsetting to rows of interested and ordering of those is expected to be done outside of this functions, e.g. x[1:2,]. This will plot data of all col.var present.

Value

a ggplot object or list of two ggplot objects, if col.var are added to the plot.

Examples

data(GlobalPatterns, package="mia")
tse <- GlobalPatterns

## If rank is set to NULL (default), agglomeration is not done. However, note
## that there is maximum number of rows that can be plotted. That is why
## we take sample from the data.
set.seed(26348)
sample <- sample(rownames(tse), 20)
tse_sub <- tse[sample, ]
# Apply relative transformation
tse_sub <- transformAssay(tse_sub, method = "relabundance")
plotAbundance(tse_sub, assay.type = "relabundance")

## Plotting counts using the first taxonomic rank as default
plotAbundance(
    tse, assay.type="counts", rank = "Phylum") +
    labs(y="Counts")

## Using "Phylum" as rank. Apply relative transformation to "counts" assay.
plotAbundance(
    tse, assay.type="counts", rank = "Phylum", add_legend = FALSE,
    as.relative = TRUE)

# Apply relative transform
tse <- transformAssay(tse, method = "relabundance")
  
## A feature from colData or taxon from chosen rank can be used for ordering
## samples.
plotAbundance(tse, assay.type="relabundance", rank = "Phylum",
           order.col.by = "Bacteroidetes")

## col.var from colData can be plotted together with abundance plot.
# Returned object is a list that includes two plot; other visualizes
## abundance other col.var. 
plot <- plotAbundance(tse, assay.type = "relabundance", rank = "Phylum",
                   col.var = "SampleType")

# These two plots can be combined with wrap_plots function from patchwork
# package
library(patchwork)
wrap_plots(plot, ncol = 1)


## Same plot as above but showing sample IDs as labels for the x axis on the
## top plot
plot[[1]] <- plotAbundance(tse, assay.type = "relabundance", rank = "Phylum",
                           col.var = "SampleType", add.legend = FALSE,
                           add.x.text = TRUE)[[1]] +
                           theme(axis.text.x = element_text(angle = 90)) 

wrap_plots(plot, ncol = 1, heights = c(0.8,0.2))


## Compositional barplot with top 5 taxa and samples sorted by
## "Bacteroidetes"

# Getting top taxa on a Phylum level
tse <- transformAssay(tse, method="relabundance")
tse_phylum <- agglomerateByRank(tse, rank ="Phylum", onRankOnly=TRUE)
top_taxa <- getTop(tse_phylum,top = 5, assay.type = "relabundance")

# Renaming the "Phylum" rank to keep only top taxa and the rest to "Other"
phylum_renamed <- lapply(rowData(tse)$Phylum,
                       function(x){if (x %in% top_taxa) {x} else {"Other"}})
rowData(tse)$Phylum <- as.character(phylum_renamed)

# Compositional barplot
plotAbundance(tse, assay.type="relabundance", rank = "Phylum",
           order.row.by="abund", order.col.by = "Bacteroidetes")

microbiome/miaViz documentation built on Aug. 13, 2024, 8:57 p.m.