A hierarchically-clustered expression heatmap (in the spirit of
seaborn.clustermap / Morpheus): the matrix is drawn on a GPU/canvas data
layer so large matrices stay smooth, while dendrograms, tick labels and the
colorbar are crisp vector overlays. Rows and columns are agglomeratively
clustered and reordered so structure appears as blocks along the diagonal.
A matrix with more rows or columns than the plot has pixels is pooled for
drawing: each pixel shows the mean of the cells under it, so a lone
outlier row still shows, fainter the more rows share its pixel.
Usage
clustermap(
mat,
metric = c("euclidean", "correlation"),
linkage = c("average", "complete", "ward"),
colormap = NULL,
z_score = FALSE,
cluster_rows = TRUE,
cluster_cols = TRUE,
show_row_dendrogram = TRUE,
show_col_dendrogram = TRUE,
show_labels = TRUE,
legend_title = "value",
row_linkage = NULL,
col_linkage = NULL,
theme = NULL,
width = NULL,
height = NULL,
element_id = NULL
)Arguments
- mat
A numeric matrix. Row and column names, if present, are used as labels. Values are transported row-major to the browser. A missing cell (
NA,NaNor an infinite value) is drawn grey, and distances between rows or columns use the cells both have, scaled up asstats::dist()does. Where several rows or columns share a pixel, the pixel shows the mean of their finite cells, so it is grey only when all of them are missing.- metric
Distance metric for clustering:
"euclidean"or"correlation"(1 - Pearson correlation).- linkage
Agglomeration method:
"average","complete"or"ward"."ward"is Ward's method ashclust(method = "ward.D2")and scipy compute it, and it assumes the euclidean metric (a warning says so for correlation).- colormap
Color ramp: sequential
"viridis"or"ltc"(an earthy teal to sand to rust ramp), or diverging"rdbu"or"ltcdiv"(its counterpart, neutral cream at the midpoint), whose range is symmetric about zero.NULL(the default) uses"rdbu"whenz_scoreisTRUE, so values above and below the row mean get opposite colours, and"viridis"otherwise.- z_score
Standardize each row to mean 0 / sd 1 before coloring.
- cluster_rows, cluster_cols
Cluster and reorder rows / columns. Ignored for an axis when a precomputed
row_linkage/col_linkageis supplied.- show_row_dendrogram, show_col_dendrogram
Draw the row / column dendrogram.
- show_labels
Draw row/column tick labels (auto-hidden when cells get too small to be legible).
- legend_title
Colorbar legend title.
- row_linkage, col_linkage
Optional precomputed leaf order or dendrogram to skip clustering that axis. Either an integer vector giving the 0-based leaf order, or a list with
order(0-based) andmerges(each a list withleft,right,height; leaves are0..n-1, internal nodekisn + k).- theme
Optional named list of theme overrides (colors, fonts, ...) merged over the component defaults in the browser.
NULLuses the default theme. Itspaddingentry is the space in px between the edge of the widget and the plot: one number for every side, or numbers namedtop,right,bottomandleftin a list or vector, a side left out keeping the default of 10. A colour given as a CSS custom property, such asbackground = "var(--bg)", takes the value the page gives it when the plot draws. After the page changes the property, what the plot drew on a canvas keeps the old colour until it draws again: resize the widget or render it again.- width, height
Widget dimensions (any valid CSS size).
- element_id
Optional explicit DOM id.
Details
Clustering is at least O(n^2) in the number of rows/columns (it builds a
full distance matrix), so clustermap() only clusters automatically when a
dimension has at most 2000 leaves. For larger matrices, precompute a leaf
order (or a dendrogram) elsewhere and pass it via row_linkage /
col_linkage to skip clustering; the heatmap rendering itself scales to
much larger matrices.
Examples
set.seed(1)
# Two clear blocks of correlated genes across two groups of samples.
mat <- rbind(
matrix(rnorm(20 * 10, mean = 2), nrow = 20),
matrix(rnorm(20 * 10, mean = -2), nrow = 20)
)
rownames(mat) <- paste0("gene", seq_len(nrow(mat)))
colnames(mat) <- paste0("s", seq_len(ncol(mat)))
clustermap(mat, colormap = "rdbu", z_score = TRUE)