R/AllGenerics.R, R/plotLoadings.R
plotLoadings.RdThis function is used after performing a reduction method. If TreeSE
is given it retrieves the feature loadings matrix to plot values.
A tree from rowTree can be added to heatmap layout.
plotLoadings(x, ...)
# S4 method for class 'TreeSummarizedExperiment'
plotLoadings(
x,
dimred,
layout = "lollipop",
ncomponents = 5,
tree.name = "phylo",
row.var = NULL,
add.tree = FALSE,
...
)
# S4 method for class 'SingleCellExperiment'
plotLoadings(x, dimred, layout = "lollipop", ncomponents = 5, ...)
# S4 method for class 'matrix'
plotLoadings(x, layout = "lollipop", ncomponents = 5, ...)additional parameters for plotting.
n: Integer scalar. Number of features to be plotted.
Applicable when layout="barplot" or layout="lollipop".
(Default: min(nrow(x), 10L)))
absolute.scale: ("barplot", "lollipop") Logical scalar.
Specifies whether a barplot or a lollipop plot should be visualized in
absolute scale. (Default: TRUE)
sort.method: ("heatmap") NULL, Character
scalar or BlusterParam object. Specifies how features are
sorted. If it specifies a column, i.e., principal coordinate, features
are ordered in decreasing order based on the specified coordinate. Other
character values specify a seriation method, e.g.,
"PCA_angle" or "Heatmap", and BlusterParam
objects specify a clustering method. See
getHeatmapOrder for details. If NULL, features are
not sorted. (Default: NULL)
Character scalar. Determines the reduced dimension to
plot.
Character scalar. Determines the layout of plot. Must be
either "lollipop", "heatmap", or "barplot".
(Default: "lollipop")
Numeric scalar. Number of components must be lower
or equal to the number of components chosen in the reduction method.
(Default: 5)
Character scalar. Specifies a rowTree/colTree from
x. (Default: tree.name = "phylo")
NULL or Character scalar. Specifies a
variable from rowData to plot with tree heatmap layout.
(Default: NULL)
Logical scalar. Whether to add tree to heatmap layout.
(Default: FALSE)
A ggplot2 object.
These method visualize feature loadings of dimension reduction results.
Inspired by the plotASVcircular method using phyloseq.
TreeSummarizedExperiment object is expected to have
content in reducedDim slot calculated with standardized methods from
mia or scater package.
The components are shown with the column names of the loadings matrix. If
the matrix has no column names, or it has the default names "V1",
"V2", ..., the components are labelled "Component 1",
"Component 2", ... In barplot and lollipop layouts, the legend
showing the sign of the loadings is omitted when all the loadings have the
same sign; it can be shown with, e.g.,
+ guides(colour = "legend") (lollipop) or
+ guides(fill = "legend") (barplot).
library(mia)
library(scater)
data("GlobalPatterns", package = "mia")
tse <- GlobalPatterns
# Calculate PCA
tse <- agglomerateByRank(tse, rank = "Phylum", update.tree = TRUE)
#> Warning: The tree is removed because it cannot be agglomerated. Agglomeration requires that each group is monophyletic, i.e., the features of a group form a subtree of their own. 5 group(s) are not monophyletic: 'Bacteria_Actinobacteria', 'Bacteria_Proteobacteria', 'Bacteria_Chlorobi', 'Bacteria_Firmicutes', 'Bacteria_SC4'.
tse <- subsetBy(
tse, rows = rowMeans(counts > 0) > 0.5, collapse = TRUE,
update.tree = TRUE)
tse <- transformAssay(tse, method = "clr", pseudocount = 1)
tse <- runPCA(tse, ncomponents = 5, assay.type = "clr")
# Plotting feature loadings
plotLoadings(tse, dimred = "PCA", layout = "heatmap", add.tree = FALSE) |>
# Remove this line to see messages
suppressMessages()
# Plotting matrix as a barplot
loadings_matrix <- attr(reducedDim(tse, "PCA"), "rotation")
plotLoadings(loadings_matrix)
# Plotting more features but less components
plotLoadings(tse, dimred = "PCA", ncomponents = 2, n = 12)
# Plotting matrix as heatmap without tree
plotLoadings(loadings_matrix, layout = "heatmap")
# Plot with less components
plotLoadings(
tse, "PCA", layout = "heatmap",
ncomponents = 2, sort.method = "PCA_angle"
)