| Title: | Heatmaps for SummarizedExperiment Objects |
|---|---|
| Description: | Represents a heatmap as a SummarizedExperiment-derived object, holding a single assay together with its row and column annotations and any hierarchical clustering. Assay values, annotations, display order, and dendrograms stay synchronized as the object is subsetted or reclustered, so a heatmap's components cannot drift out of step with each other. Each visual component (the main heatmap, annotation strips, dendrograms) is returned as an ordinary ggplot2 plot, and these can be composed, laid out, and customized using patchwork. |
| Authors: | Bradley Blaser [aut, cre] (ORCID: <https://orcid.org/0000-0002-3168-5423>) |
| Maintainer: | Bradley Blaser <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.99.0 |
| Built: | 2026-10-13 19:42:19 UTC |
| Source: | https://github.com/bioc/SummarizedHeatmap |
A SummarizedExperiment-derived representation of clustered assays and
their feature and sample annotations. Plotting functions return ordinary
ggplot2 and patchwork objects that remain open to standard customization.
Maintainer: Bradley Blaser [email protected] (ORCID)
Authors:
Bradley Blaser [email protected] (ORCID)
SummarizedHeatmap(), plotHeatmap(),
SummarizedExperiment::SummarizedExperiment()
Subsetting follows SummarizedExperiment semantics. If row membership or
order changes, the row dendrogram is cleared and row order resets to the
natural order of the subset. The same rule applies independently to
columns. Clustering for an unchanged dimension is preserved. Subsetting
never reclusters implicitly; call clusterRows(), clusterCols(), or
clusterHeatmap() when new clustering is desired.
## S4 method for signature 'SummarizedHeatmap,ANY,ANY,ANY' x[i, j, ..., drop = FALSE]## S4 method for signature 'SummarizedHeatmap,ANY,ANY,ANY' x[i, j, ..., drop = FALSE]
x |
A SummarizedHeatmap object. |
i |
Row indices, names, or logical selector. |
j |
Column indices, names, or logical selector. |
... |
Additional subsetting arguments. |
drop |
Whether to drop dimensions. Defaults to |
A valid SummarizedHeatmap object.
mat <- matrix(rnorm(20), 5, dimnames = list(letters[1:5], LETTERS[1:4])) x <- SummarizedHeatmap(mat) x[1:3, 1:2]mat <- matrix(rnorm(20), 5, dimnames = list(letters[1:5], LETTERS[1:4])) x <- SummarizedHeatmap(mat) x[1:3, 1:2]
Assay replacement follows SummarizedExperiment semantics: dimensions and
dimnames must be preserved. Because a stored dendrogram and its implied
order are only valid for the assay values they were computed from,
replacing the assay never silently reclusters. Instead, whenever a
dendrogram is currently stored for an axis, it is cleared and that axis's
order resets to its natural order; when no dendrogram is stored (e.g. a
manually-supplied or default order), the existing order is left
untouched. The same rule is applied independently to rows and columns.
Call clusterRows(), clusterCols(), or clusterHeatmap() afterwards to
recompute clustering against the new values.
## S4 replacement method for signature 'SummarizedHeatmap,missing' assay(x, i, withDimnames = TRUE, ...) <- value ## S4 replacement method for signature 'SummarizedHeatmap,numeric' assay(x, i, withDimnames = TRUE, ...) <- value ## S4 replacement method for signature 'SummarizedHeatmap,character' assay(x, i, withDimnames = TRUE, ...) <- value## S4 replacement method for signature 'SummarizedHeatmap,missing' assay(x, i, withDimnames = TRUE, ...) <- value ## S4 replacement method for signature 'SummarizedHeatmap,numeric' assay(x, i, withDimnames = TRUE, ...) <- value ## S4 replacement method for signature 'SummarizedHeatmap,character' assay(x, i, withDimnames = TRUE, ...) <- value
x |
A SummarizedHeatmap object. |
i |
Optional name or index of the assay to replace. Since a
|
withDimnames |
A logical value indicating whether |
... |
Additional arguments passed to the |
value |
A numeric, non-complex matrix with the same dimensions and dimnames as the current assay. |
An updated, valid SummarizedHeatmap object.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) x <- SummarizedHeatmap(mat) assay(x) <- mat * 2 rowDendro(x)mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) x <- SummarizedHeatmap(mat) assay(x) <- mat * 2 rowDendro(x)
Assay replacement follows SummarizedExperiment semantics: dimensions and
dimnames must be preserved. Because a stored dendrogram and its implied
order are only valid for the assay values they were computed from,
replacing the assay never silently reclusters. Instead, whenever a
dendrogram is currently stored for an axis, it is cleared and that axis's
order resets to its natural order; when no dendrogram is stored (e.g. a
manually-supplied or default order), the existing order is left
untouched. The same rule is applied independently to rows and columns.
Call clusterRows(), clusterCols(), or clusterHeatmap() afterwards to
recompute clustering against the new values.
## S4 replacement method for signature 'SummarizedHeatmap,list' assays(x, withDimnames = TRUE, ...) <- value ## S4 replacement method for signature 'SummarizedHeatmap,SimpleList' assays(x, withDimnames = TRUE, ...) <- value## S4 replacement method for signature 'SummarizedHeatmap,list' assays(x, withDimnames = TRUE, ...) <- value ## S4 replacement method for signature 'SummarizedHeatmap,SimpleList' assays(x, withDimnames = TRUE, ...) <- value
x |
A SummarizedHeatmap object. |
withDimnames |
A logical value indicating whether |
... |
Additional arguments passed to the |
value |
A list or |
An updated, valid SummarizedHeatmap object.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) x <- SummarizedHeatmap(mat) assays(x) <- list(matrix = mat * 2) rowDendro(x)mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) x <- SummarizedHeatmap(mat) assays(x) <- list(matrix = mat * 2) rowDendro(x)
Compute a hierarchical clustering across assay columns and update the stored column dendrogram and display order.
clusterCols(x, distMethod = "euclidean", hclustMethod = "average")clusterCols(x, distMethod = "euclidean", hclustMethod = "average")
x |
A SummarizedHeatmap object. |
distMethod |
Distance method passed to |
hclustMethod |
Linkage method passed to |
The updated SummarizedHeatmap object.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) clusterCols(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) clusterCols(SummarizedHeatmap(mat))
Cluster rows and columns
clusterHeatmap(x, distMethod = "euclidean", hclustMethod = "average")clusterHeatmap(x, distMethod = "euclidean", hclustMethod = "average")
x |
A SummarizedHeatmap object. |
distMethod |
Distance method passed to |
hclustMethod |
Linkage method passed to |
The updated SummarizedHeatmap object.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) clusterHeatmap(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) clusterHeatmap(SummarizedHeatmap(mat))
Compute a hierarchical clustering across assay rows and update the stored row dendrogram and display order. Clustering is deterministic for a fixed assay and set of methods.
clusterRows(x, distMethod = "euclidean", hclustMethod = "average")clusterRows(x, distMethod = "euclidean", hclustMethod = "average")
x |
A SummarizedHeatmap object. |
distMethod |
Distance method passed to |
hclustMethod |
Linkage method passed to |
The updated SummarizedHeatmap object.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) clusterRows(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) clusterRows(SummarizedHeatmap(mat))
Access stored column dendrogram
colDendro(x) ## S4 method for signature 'SummarizedHeatmap' colDendro(x)colDendro(x) ## S4 method for signature 'SummarizedHeatmap' colDendro(x)
x |
A SummarizedHeatmap object. |
A dendrogram, or NULL if columns are not clustered.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) colDendro(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) colDendro(SummarizedHeatmap(mat))
Access column display order
colOrder(x) ## S4 method for signature 'SummarizedHeatmap' colOrder(x)colOrder(x) ## S4 method for signature 'SummarizedHeatmap' colOrder(x)
x |
A SummarizedHeatmap object. |
Integer column indices in display order.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) colOrder(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) colOrder(SummarizedHeatmap(mat))
Each selected column annotation is represented by an independent ggplot,
labelled with its variable name as axis text next to the annotation
tiles. A single variable returns a ggplot; multiple variables return an
(unwrapped) patchwork stacking the independent plots (see
plotRowData() for alignment and safe-composition details).
plotColData( x, vars = NULL, side = "right", tileColor = "white", palette = NULL, showNames = FALSE )plotColData( x, vars = NULL, side = "right", tileColor = "white", palette = NULL, showNames = FALSE )
x |
A SummarizedHeatmap object. |
vars |
Character vector of |
side |
Which side of the annotation tiles the variable-name label is
drawn on: |
tileColor |
Tile border colour. |
palette |
Optional named values passed to |
showNames |
Whether to display row or column names on the annotation axis. |
A ggplot for one variable, a patchwork for multiple variables, or a spacer when no column annotations are present.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) cd <- S4Vectors::DataFrame( group = c("A", "B", "A"), row.names = LETTERS[1:3] ) plotColData(SummarizedHeatmap(mat, colData = cd))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) cd <- S4Vectors::DataFrame( group = c("A", "B", "A"), row.names = LETTERS[1:3] ) plotColData(SummarizedHeatmap(mat, colData = cd))
Plot the stored column dendrogram
plotColDendro(x, side = "top", linewidth = 0.5)plotColDendro(x, side = "top", linewidth = 0.5)
x |
A SummarizedHeatmap object. |
side |
Side on which the dendrogram is oriented. The default places leaves toward the heatmap when shown to its left. |
linewidth |
Dendrogram line width. |
A ggplot object.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) plotColDendro(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) plotColDendro(SummarizedHeatmap(mat))
The component plots remain available through their individual plotting
functions. This helper arranges them around the main heatmap panel,
constructing the components in a deterministic order (the main heatmap,
then column dendrogram, column annotation, then row dendrogram, row
annotation) so that, when collectGuides = TRUE, their legends are always
collected into the shared guide area in that same order, regardless of
which side of the heatmap each panel is placed on. guide_area() (the M
panel) only controls where the collected guides are drawn, not the order
they are collected in; see .orderPanelsForGuides() for the ordering
itself.
Column annotation plots are stacked vertically. The row dendrogram and row
annotation panels move to whichever side of the heatmap rowDendroSide/
rowAnnotationSide request ("left" or "right", both defaulting to
"left"); the column dendrogram and column annotation panels likewise
move with colDendroSide/colAnnotationSide ("top" or "bottom", both
defaulting to "top"). When a dendrogram and its matching annotation
share a side, the annotation sits adjacent to the heatmap and the
dendrogram sits further out; by default this puts both on the same side
for each axis. Pass named variable vectors to rowVars or colVars to
control annotation strip labels; pass NULL to rowAnnotationSide/
colAnnotationSide to omit that annotation panel entirely. Any omitted
panel – an annotation set to NULL, a dendrogram hidden with
showRowDendro/showColDendro = FALSE, or a dendrogram that was never
stored – is dropped from the grid rather than left as a blank but
still-sized slot, so the remaining panels use the reclaimed space.
plotHeatmap( x, rowVars = NULL, colVars = NULL, showRowDendro = TRUE, showColDendro = TRUE, rowDendroSide = "left", colDendroSide = "top", rowAnnotationSide = "left", colAnnotationSide = "top", collectGuides = TRUE, guideWidth = 3, ... )plotHeatmap( x, rowVars = NULL, colVars = NULL, showRowDendro = TRUE, showColDendro = TRUE, rowDendroSide = "left", colDendroSide = "top", rowAnnotationSide = "left", colAnnotationSide = "top", collectGuides = TRUE, guideWidth = 3, ... )
x |
A SummarizedHeatmap object. |
rowVars, colVars
|
Optional annotation variables to display. |
showRowDendro, showColDendro
|
Whether to display stored dendrograms. |
rowDendroSide |
|
colDendroSide |
|
rowAnnotationSide, colAnnotationSide
|
Which side of the heatmap the
annotation panel is placed on, restricted to |
collectGuides |
Collect guides into a shared guide area. |
guideWidth |
Relative width of the guide area column, on the same
scale as the dendrogram/annotation ( |
... |
Additional arguments passed to |
Each annotation strip's variable-name axis text is wrapped with
patchwork::free(type = "space") so it packs tightly against the heatmap
instead of forcing extra column/row width (see plotColData()/
plotRowData() for the axis text itself). Because "space" reserves no
room for that text, it can get clipped at the edge of the plotting device
if there isn't enough surrounding space to draw it – most likely with the
non-default rowAnnotationSide = "right", which anchors the row
annotation's variable-name label at the bottom of its strip, where it
competes for space with the heatmap's own column identifier labels (the
column annotation's variable-name label always sits to the right of its
strip, matching the heatmap's own row labels, regardless of
colAnnotationSide). Increase the figure's height or width if labels are
clipped in that configuration.
A patchwork object.
mat <- matrix( rnorm(24), 6, dimnames = list(paste0("f", 1:6), paste0("s", 1:4)) ) x <- SummarizedHeatmap(mat) plotHeatmap(x)mat <- matrix( rnorm(24), 6, dimnames = list(paste0("f", 1:6), paste0("s", 1:4)) ) x <- SummarizedHeatmap(mat) plotHeatmap(x)
The returned object is an ordinary ggplot and can be modified with standard ggplot2 layers and scales. The display follows the stored row and column orders.
plotHeatmapMain( x, tileColor = "white", high = "red3", mid = "white", low = "blue4", midpoint = 0, fillTitle = "Value", flip = FALSE )plotHeatmapMain( x, tileColor = "white", high = "red3", mid = "white", low = "blue4", midpoint = 0, fillTitle = "Value", flip = FALSE )
x |
A SummarizedHeatmap object. |
tileColor |
Tile border colour. |
high, mid, low
|
Colours for high, midpoint, and low assay values. |
midpoint |
Assay value mapped to |
fillTitle |
Title for the fill legend. |
flip |
If |
A ggplot object.
mat <- matrix( rnorm(24), 6, dimnames = list(paste0("f", 1:6), paste0("s", 1:4)) ) plotHeatmapMain(SummarizedHeatmap(mat))mat <- matrix( rnorm(24), 6, dimnames = list(paste0("f", 1:6), paste0("s", 1:4)) ) plotHeatmapMain(SummarizedHeatmap(mat))
Each selected row annotation is represented by an independent ggplot,
labelled with its variable name as axis text next to the annotation tiles
(in addition to its legend title). A single variable returns a ggplot;
multiple variables return an (unwrapped) patchwork stacking the
independent plots, so the result stays aligned with sibling panels and
its guides can still be collected by an enclosing plot_layout(guides = "collect") – see plotHeatmap().
plotRowData( x, vars = NULL, side = "top", tileColor = "white", palette = NULL, showNames = FALSE )plotRowData( x, vars = NULL, side = "top", tileColor = "white", palette = NULL, showNames = FALSE )
x |
A SummarizedHeatmap object. |
vars |
Character vector of |
side |
Which side of the annotation tiles the variable-name label is
drawn on: |
tileColor |
Tile border colour. |
palette |
Optional named values passed to |
showNames |
Whether to display row or column names on the annotation axis. |
Combining a multiple-variable result directly with another plot using
patchwork operators (+, /, |) can silently flatten its panel list
into the parent's, which may break a plot_layout(heights = ...)/ plot_layout(design = ...) built around an expected panel count. Use
patchwork::wrap_plots(list(annotation, otherPlot), ...) instead of an
operator to combine them safely (this is how plotHeatmap() does it
internally).
When composing a custom layout, wrap the result with
patchwork::free(type = "space") so its variable-name axis text packs
tightly against neighbouring panels instead of forcing extra column/row
width (plotHeatmap() does this for every annotation strip). Because
"space" reserves no room for that text, it can get clipped at the edge
of the plotting device when there isn't enough surrounding space – most
likely with side = "right" or side = "bottom", where the (possibly
rotated) label competes for space with other axis labels. Increase the
figure's height or width if labels are clipped in that configuration.
A ggplot for one variable, a patchwork for multiple variables, or a spacer when no row annotations are present.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) rd <- S4Vectors::DataFrame( kind = rep(c("gene", "control"), 2), row.names = letters[1:4] ) plotRowData(SummarizedHeatmap(mat, rowData = rd))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) rd <- S4Vectors::DataFrame( kind = rep(c("gene", "control"), 2), row.names = letters[1:4] ) plotRowData(SummarizedHeatmap(mat, rowData = rd))
Plot the stored row dendrogram
plotRowDendro(x, side = "left", linewidth = 0.5)plotRowDendro(x, side = "left", linewidth = 0.5)
x |
A SummarizedHeatmap object. |
side |
Side on which the dendrogram is oriented. The default places leaves toward the heatmap when shown to its left. |
linewidth |
Dendrogram line width. |
A ggplot object.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) plotRowDendro(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) plotRowDendro(SummarizedHeatmap(mat))
Access stored row dendrogram
rowDendro(x) ## S4 method for signature 'SummarizedHeatmap' rowDendro(x)rowDendro(x) ## S4 method for signature 'SummarizedHeatmap' rowDendro(x)
x |
A SummarizedHeatmap object. |
A dendrogram, or NULL if rows are not clustered.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) rowDendro(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) rowDendro(SummarizedHeatmap(mat))
Access row display order
rowOrder(x) ## S4 method for signature 'SummarizedHeatmap' rowOrder(x)rowOrder(x) ## S4 method for signature 'SummarizedHeatmap' rowOrder(x)
x |
A SummarizedHeatmap object. |
Integer row indices in display order.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) rowOrder(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) rowOrder(SummarizedHeatmap(mat))
Display a concise summary of a SummarizedHeatmap
## S4 method for signature 'SummarizedHeatmap' show(object)## S4 method for signature 'SummarizedHeatmap' show(object)
object |
A SummarizedHeatmap object. |
The method displays a summary and returns invisibly.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) show(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) show(SummarizedHeatmap(mat))
Create a heatmap representation from a numeric matrix or from a single
assay of a SummarizedExperiment object. Rows and columns are
clustered by default when their sizes permit; stored orders are integer
indices into the assay dimensions. Missing dimension names are filled with
stable sequential identifiers when x is a matrix.
SummarizedHeatmap( x, assay = NULL, rowOrder = NULL, colOrder = NULL, clusterRows = TRUE, clusterCols = TRUE, distMethod = "euclidean", hclustMethod = "average", ... )SummarizedHeatmap( x, assay = NULL, rowOrder = NULL, colOrder = NULL, clusterRows = TRUE, clusterCols = TRUE, distMethod = "euclidean", hclustMethod = "average", ... )
x |
A numeric matrix, or a SummarizedExperiment object. |
assay |
When |
rowOrder |
Optional manual row order, as integer indices or row names.
Supplying it leaves the row dendrogram unset and takes precedence over
|
colOrder |
Optional manual column order, as integer indices or column
names. Supplying it leaves the column dendrogram unset and takes
precedence over |
clusterRows |
Whether to compute a row dendrogram and clustering
order. Ignored (with no dendrogram computed) when |
clusterCols |
Whether to compute a column dendrogram and clustering
order. Ignored (with no dendrogram computed) when |
distMethod |
Distance method passed to |
hclustMethod |
Linkage method passed to |
... |
Additional arguments passed to
|
A SummarizedHeatmap always holds exactly one assay. When x is a
SummarizedExperiment with a single assay, that assay is used
automatically. When it has multiple assays, assay must be supplied to
select one by name or position. Either way, the selected assay's original
name is preserved in the resulting SummarizedHeatmap; rowData,
colData, and metadata are carried over from x unchanged.
A valid SummarizedHeatmap object.
mat <- matrix(rnorm(24), nrow = 6, dimnames = list(paste0("feature", 1:6), paste0("sample", 1:4)) ) x <- SummarizedHeatmap(mat) validObject(x) se <- SummarizedExperiment::SummarizedExperiment( assays = list(counts = mat) ) y <- SummarizedHeatmap(se) validObject(y) SummarizedExperiment::assayNames(y) # "counts", preserved from semat <- matrix(rnorm(24), nrow = 6, dimnames = list(paste0("feature", 1:6), paste0("sample", 1:4)) ) x <- SummarizedHeatmap(mat) validObject(x) se <- SummarizedExperiment::SummarizedExperiment( assays = list(counts = mat) ) y <- SummarizedHeatmap(se) validObject(y) SummarizedExperiment::assayNames(y) # "counts", preserved from se
A single assay and its row and column annotations, display orders, and
optional hierarchical clusterings. The class extends
SummarizedExperiment but is restricted to exactly one assay; use
SummarizedHeatmap() to build one from a matrix or from a
SummarizedExperiment (selecting an assay when it holds more than one).
rowDendroRow dendrogram, or NULL when rows are not clustered.
colDendroColumn dendrogram, or NULL when columns are not clustered.
rowOrderInteger indices giving row display order.
colOrderInteger indices giving column display order.
mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) show(SummarizedHeatmap(mat))mat <- matrix(rnorm(12), 4, dimnames = list(letters[1:4], LETTERS[1:3])) show(SummarizedHeatmap(mat))