library(ComplexHeatmap)
set.seed(123)
= 4; nr2 = 8; nr3 = 6; nr = nr1 + nr2 + nr3
nr1 = 6; nc2 = 8; nc3 = 10; nc = nc1 + nc2 + nc3
nc1 = cbind(rbind(matrix(rnorm(nr1*nc1, mean = 1, sd = 0.5), nr = nr1),
mat matrix(rnorm(nr2*nc1, mean = 0, sd = 0.5), nr = nr2),
matrix(rnorm(nr3*nc1, mean = 0, sd = 0.5), nr = nr3)),
rbind(matrix(rnorm(nr1*nc2, mean = 0, sd = 0.5), nr = nr1),
matrix(rnorm(nr2*nc2, mean = 1, sd = 0.5), nr = nr2),
matrix(rnorm(nr3*nc2, mean = 0, sd = 0.5), nr = nr3)),
rbind(matrix(rnorm(nr1*nc3, mean = 0.5, sd = 0.5), nr = nr1),
matrix(rnorm(nr2*nc3, mean = 0.5, sd = 0.5), nr = nr2),
matrix(rnorm(nr3*nc3, mean = 1, sd = 0.5), nr = nr3))
)= mat[sample(nr, nr), sample(nc, nc)] # random shuffle rows and columns
mat rownames(mat) = paste0("row", seq_len(nr))
colnames(mat) = paste0("column", seq_len(nc))
Heatmap(mat, cluster_rows = FALSE, cluster_columns = FALSE)
The ComplexHeatmap Bioconductor package (Gu, Eils, and Schlesner 2016; Gu 2021a, 2021b) has become my goto for visualizing sample-sample correlation heatmaps, which I use a lot. Recently, I had a report where I wanted to highlight a particular row of the heatmap. There is not an easy way that I could find to add something that wraps around a particular column. However, you can indicate that there is a grouping in the heatmap, and use that grouping to separate a sample or samples from the others.
Lets do an example:
Now, lets suppose we just want to highlight row2.
We create a data.frame with a factor to represent the grouping:
= which(grepl("row2", rownames(mat)))
which_row2 = data.frame(x = c(rep("A", which_row2 - 1), "B",
split rep("C", nrow(mat) - which_row2)))
Heatmap(mat, cluster_rows = FALSE, cluster_columns = FALSE,
row_split = split,
row_title = NULL)
Voila! row2 is separated from the others to draw attention to it. It’s not perfect, but hopefully it’s useful to others. Note, that you can’t use clustering with this method. If you have actual dendrograms to display, this will fail, because ComplexHeatmap expects you to use a numeric argument to tell the cut height for dendrograms for splitting (Gu 2021c). Therefore, if you have dendrograms, reorder your columns and rows according to the dendrogram first and then add the splitting information and keep the clustering off.
References
Reuse
Citation
@online{mflight2021,
author = {Robert M Flight},
title = {Highlighting a {Row} of {A} {ComplexHeatmap}},
date = {2021-03-26},
url = {https://rmflight.github.io/posts/2021-03-26-highlighting-a-row-of-a-complexheatmap},
langid = {en}
}