Creates a heatmap from an assay stored in a SummarizedExperiment or TreeSummarizedExperiment. Features are shown as rows and samples as columns.

plotHeatmap(x, ...)

# S4 method for class 'TreeSummarizedExperiment'
plotHeatmap(
  x,
  assay.type = NULL,
  row.var = NULL,
  col.var = NULL,
  scale = FALSE,
  center = FALSE,
  sort.method = NULL,
  tree.name = "phylo",
  show.tree = FALSE,
  ...
)

# S4 method for class 'SummarizedExperiment'
plotHeatmap(
  x,
  assay.type = NULL,
  row.var = NULL,
  col.var = NULL,
  scale = FALSE,
  center = FALSE,
  sort.method = NULL,
  ...
)

Arguments

x

a SummarizedExperiment object.

...

Additional parameters controlling the visualization. When show.tree = TRUE, additional arguments are passed to plotRowTree to control the appearance of the tree. Additional parameters include, for example:

assay.type

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

row.var

NULL or character vector. Variables from rowData(x) used for row facetting. (Default: NULL)

col.var

NULL or character vector. Variables from colData(x) used for column facetting. (Default: NULL)

scale

Logical scalar. Should assay values be scaled for each feature across samples? (Default: FALSE)

center

Logical scalar. Should assay values be centered for each feature across samples? (Default: FALSE)

sort.method

NULL, Character scalar or BlusterParam object. Specifies the method used to sort rows and columns, e.g., a seriation method such as "PCA_angle" or "Heatmap". See getHeatmapOrder for details. If NULL, rows and columns are not sorted. (Default: NULL)

tree.name

Character scalar. Name of the row tree to display when x is a TreeSummarizedExperiment. (Default: "phylo")

show.tree

Logical scalar. Should the row tree be displayed? Only available for TreeSummarizedExperiment. (Default: TRUE)

Value

A ggplot2 object. If show.tree = TRUE, the returned object is a combined patchwork object containing the row tree and the heatmap.

Details

plotHeatmap visualizes values from an assay as a heatmap. Values can be optionally centered and/or scaled across samples for each feature, which is useful for highlighting relative abundance patterns rather than absolute abundances.

Additional variables from rowData(x) and colData(x) can be used to facet the heatmap. When multiple row or column variables are provided, nested facets are created using ggh4x.

For TreeSummarizedExperiment objects, a row tree can be displayed alongside the heatmap. When a tree is shown, only leaf nodes are plotted and the heatmap rows are reordered to match the tree tip order.

Rows and columns are shown in the order they appear in x. They can be sorted with sort.method so that similar features and similar samples are placed next to each other. The same method is used for both rows and columns. The order is determined with getHeatmapOrder from the plotted values, i.e., after optional scaling and centering. Within facets, the same order is preserved. When the row tree is shown, rows follow the tree and only columns are sorted.

Column labels are drawn vertically so that they stay readable when there are many samples. The returned object is a ggplot, so the labels can be changed afterwards, e.g., with + theme(axis.text.x = element_text(angle = 0)) or removed with + theme(axis.text.x = element_blank()).

Examples

data(GlobalPatterns)
tse <- GlobalPatterns

tse <- agglomerateByRank(tse, rank = "Class")
tse <- subsetBy(tse, rows = rowMeans(counts > 0) > 0.5, collapse = TRUE)

# Plot raw counts
plotHeatmap(
    tse,
    assay.type = "counts"
)
#> Error in plotHeatmap(tse, assay.type = "counts"): argument "features" is missing, with no default

# Scale and center each feature
plotHeatmap(
    tse,
    assay.type = "counts",
    scale = TRUE,
    center = TRUE
)
#> Error in plotHeatmap(tse, assay.type = "counts", scale = TRUE, center = TRUE): argument "features" is missing, with no default

# Facet samples by metadata
plotHeatmap(
    tse,
    assay.type = "counts",
    col.var = "SampleType"
)
#> Error in plotHeatmap(tse, assay.type = "counts", col.var = "SampleType"): argument "features" is missing, with no default

# Sort features and samples with seriation
plotHeatmap(
    tse,
    assay.type = "counts",
    scale = TRUE,
    center = TRUE,
    sort.method = "PCA_angle"
)
#> Error in plotHeatmap(tse, assay.type = "counts", scale = TRUE, center = TRUE,     sort.method = "PCA_angle"): argument "features" is missing, with no default

# Add a tree. Agglomeration drops the phylogenetic tree, so we add a tree
# based on the taxonomy.
tse <- addHierarchyTree(tse)
#> Warning: The root is added with label 'ALL'
plotHeatmap(
    tse,
    assay.type = "counts",
    show.tree = TRUE
)
#> Error in plotHeatmap(tse, assay.type = "counts", show.tree = TRUE): argument "features" is missing, with no default