plotAbundance | R Documentation |
plotAbundance()
creates a barplot of feature abundances, typically
used to visualize the relative abundance of features at a specific taxonomy
rank.
plotAbundance(x, ...)
## S4 method for signature 'SummarizedExperiment'
plotAbundance(
x,
assay.type = assay_name,
assay_name = "counts",
layout = "bar",
...
)
x |
a
|
... |
additional parameters for plotting.
See |
assay.type |
|
assay_name |
Deprecate. Use |
layout |
|
It is recommended to handle subsetting, agglomeration, and transformation
outside this function. However, agglomeration and relative transformation
can be applied using the group
and as.relative
parameters,
respectively. If one of the TAXONOMY_RANKS
is selected via
group
, mia::agglomerateByRank()
is used, otherwise
agglomerateByVariable()
is applied.
a ggplot
object or list of two
ggplot
objects, if col.var
are added to
the plot.
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", group = "Phylum") +
labs(y="Counts")
# Using "Phylum" as rank. Apply relative transformation to "counts" assay.
plotAbundance(
tse, assay.type="counts", group = "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", group = "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", group = "Phylum",
col.var = "SampleType")
# These two plots can be combined with wrap_plots function from patchwork
# package
library(patchwork)
wrap_plots(plot, ncol = 1, heights = c(0.95, 0.05))
# Same plot as above but showing sample IDs as labels for the x axis on the
# top plot. Moreover, we use facets.
plot <- plotAbundance(
tse, assay.type = "relabundance",
group = "Phylum", col.var = "SampleType", add.legend = FALSE,
add.x.text = TRUE, facet.cols = TRUE, scales = "free_x") +
theme(axis.text.x = element_text(angle = 90))
plot
# 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")
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", group = "Phylum",
order.row.by="abund", order.col.by = "Bacteroidetes")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.