Show Menu
Cheatography

ComplexHeatmap Complete Reference Cheat Sheet (DRAFT) by

Complex heatmaps are efficient to visualize associations between different sources of data sets and reveal potential patterns. Here the ComplexHeatmap package provides a highly flexible way to arrange multiple heatmaps and supports self-defined annotation graphics.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

1.1 General design

A single heatmap is composed of the heatmap body and the heatmap compon­ents. The heatmap body can be split by rows and columns. The heatmap components are titles, dendro­grams, matrix names and heatmap annota­tions, which are put on the four sides of the heamap body. The heatmap components are reordered or split according to the heatmap body.

3 Heatmap Annota­tions

A simple usage of heatmap annota­tions
column_ha = Heatma­pAn­not­ati­on(foo1 = runif(10), bar1 = anno_b­arp­lot­(ru­nif­(10)))
row_ha = rowAnn­ota­tio­n(foo2 = runif(10), bar2 = anno_b­arp­lot­(ru­nif­(10)))
Heatma­p(mat, name = "­mat­", top_an­not­ation = column_ha, right_­ann­otation = row_ha)
Color of annota­tions
librar­y(c­irc­lize)
col_fun = colorR­amp­2(c(0, 5, 10), c("b­lue­", "­whi­te", "­red­"))
ha = Heatma­pAn­not­ati­on(foo = 1:10, col = list(foo = col_fun))

ha = Heatma­pAn­not­ati­on(bar = sample­(le­tte­rs[­1:3], 10, replace = TRUE),
col = list(bar = c("a­" = "­red­", "­b" = "­gre­en", "­c" = "­blu­e")))
specify more than one vectors
ha = Heatma­pAn­not­ation(
foo = 1:10,
bar = sample­(le­tte­rs[­1:3], 10, replace = TRUE),
col = list(foo = col_fun,
bar = c("a­" = "­red­", "­b" = "­gre­en", "­c" = "­blu­e")))
annotation table by df
anno_df = data.f­ram­e(foo = 1:10,
bar = sample­(le­tte­rs[­1:3], 10, replace = TRUE))
ha = Heatma­pAn­not­ati­on(df = anno_df,
col = list(foo = col_fun,
bar = c("a­" = "­red­", "­b" = "­gre­en", "­c" = "­blu­e")))
anno_s­imple() makes heatma­p-like annota­tions
ha = Heatma­pAn­not­ati­on(foo = anno_s­imp­le(­1:10, pch = 1,
pt_gp = gpar(col = "­red­"), pt_size = unit(1:10, "­mm")))
3.4 Block annotation
Heatma­p(m­atr­ix(­rno­rm(­100), 10), name = "­mat­",
top_an­not­ation = Heatma­pAn­not­ati­on(foo = anno_b­lock(gp = gpar(fill = 2:4))),
column_km = 3)

** other annotation graphics
ha = Heatma­pAn­not­ati­on(foo = anno_p­oin­ts(­run­if(­10))) #3.6 Points annota­tion**
ha = Heatma­pAn­not­ati­on(foo = anno_l­ine­s(r­uni­f(10))) #3.7 Lines annotation
ha = Heatma­pAn­not­ati­on(foo = anno_b­arp­lot­(1:10)) #3.8 Barplot annotation
## a barplot annotation which visualizes a proportion matrix
m = matrix­(ru­nif­(4*10), nc = 4)
m = t(apply(m, 1, functi­on(x) x/sum(x)))
ha = Heatma­pAn­not­ati­on(foo = anno_b­arp­lot(m, gp = gpar(fill = 2:5),
bar_width = 1, height = unit(6, "­cm")))
ha = Heatma­pAn­not­ati­on(foo = anno_b­oxp­lot(m, height = unit(4, "­cm"))) #3.9 Boxplot annotation
ha = rowAnn­ota­tio­n(foo = anno_h­ist­ogr­am(m)) # apply
m
on rows #3.10 Histogram annotation
3.18 Multiple annota­tions
ha = Heatma­pAn­not­ati­on(foo = 1:10,
bar = cbind(­1:10, 10:1),
pt = anno_p­oin­ts(­1:10),
show_l­egend = c("b­ar" = FALSE))
Heatma­p(m­atr­ix(­rno­rm(­100), 10), name = "­mat­", top_an­not­ation = ha)

4 A List of Heatmaps

The main feature of Comple­xHe­atmap package is it supports to concat­enate a list of heatmaps and annota­tions horizo­ntally or vertically so that it makes it possible to visualize the associ­ations from various sources of inform­ation.
concat­enate heatmaps
ht1 = Heatma­p(mat1, name = "­rno­rm")
ht2 = Heatma­p(mat2, name = "­run­if")
ht3 = Heatma­p(le, name = "­let­ter­s")
ht1 + ht2 + ht3
4.1 Titles
draw(h­t_list, row_title = "­Three heatmaps, row title", row_ti­tle_gp = gpar(col = "­red­"),
column­_title = "­Three heatmaps, column title", column­_ti­tle_gp = gpar(f­ontsize = 16))
4.3 Gap between heatmaps
draw(h­t_list, ht_gap = unit(1, "­cm"))
4.8 Concat­enate only the annota­tions
rowAnn­ota­tio­n(foo = 1:12) +
rowAnn­ota­tio­n(bar = anno_b­arp­lot­(1:12, width = unit(4, "­cm")))
4.9 Vertical concat­enation
ht_list = ht1 %v% ht2 %v% ht3; draw(h­t_list)

2 A Single Heatmap

2.1 Colors
## contin­ueous
librar­y(c­irc­lize)
col_fun = colorR­amp­2(c(-2, 0, 2), c("g­ree­n", "­whi­te", "­red­"), space = "­RGB­")
Heatma­p(mat, name = "­mat­", col = col_fun)
## discrete
discre­te_mat = matrix­(sa­mpl­e(1:4, 100, replace = TRUE), 10, 10)
colors = struct­ure­(1:4, names = c("1­", "­2", "­3", "­4")) # black, red, green, blue
Heatma­p(d­isc­ret­e_mat, name = "­mat­", col = colors)
2.4 Set row and column orders
Heatma­p(mat, name = "­mat­", row_order = order(­as.n­um­eri­c(g­sub­("ro­w", "­", rownam­es(­mat­)))),
column­_order = order(­as.n­um­eri­c(g­sub­("co­lum­n", "­", colnam­es(­mat­)))))
2.7 Split by catego­rical variables
## split by a vector
Heatma­p(mat, name = "­mat­",
row_split = rep(c(­"­A", "­B"), 9), column­_split = rep(c(­"­C", "­D"), 12))
## split by a data frame
Heatma­p(mat, name = "­mat­",
row_split = data.f­ram­e(r­ep(­c("A­", "­B"), 9), rep(c(­"­C", "­D"), each = 9)))
## Order of slices
Heatma­p(mat, name = "­mat­",
row_split = factor­(re­p(L­ETT­ERS­[1:3], 6), levels = LETTER­S[3­:1]),
column­_split = factor­(re­p(l­ett­ers­[1:6], 4), levels = letter­s[6­:1]),
cluste­r_r­ow_­slices = FALSE,
cluste­r_c­olu­mn_­slices = FALSE)
2.12 Get orders and dendro­grams
small_mat = mat[1:9, 1:9]; ht1 = Heatma­p(s­mal­l_mat); row_or­der­(ht1)
2.13 Subset a heatmap
ht = Heatma­p(mat, name = "­mat­")
dim(ht); ht[1:10, 1:10]
 

7 OncoPrint

## If the separators are in ;:,|, oncoPr­int() automa­tically spit the alteration strings##
get_ty­pe_fun = functi­on(x) strspl­it(x, "­;")[[1]]
layer by layer style by specifying alter_fun as a list
col = c(snv = "­red­", indel = "­blu­e")
oncoPr­int­(mat,
alter_fun = list(
snv = functi­on(x, y, w, h) grid.r­ect(x, y, w0.9, h0.9,
gp = gpar(fill = col["sn­v"], col = NA)),
indel = functi­on(x, y, w, h) grid.r­ect(x, y, w0.9, h0.4,
gp = gpar(fill = col["in­del­"], col = NA))), col = col)
7.1.2 grid-b­y-grid style by specifying alter_fun as a single function
oncoPr­int­(mat,
alter_fun = functi­on(x, y, w, h, v) {
if(v["s­nv"]) grid.r­ect(x, y, w0.9, h0.9, # v["s­nv"] is a logical value
gp = gpar(fill = col["sn­v"], col = NA))
if(v["i­nde­l"]) grid.r­ect(x, y, w0.9, h0.4, # v["i­nde­l"] is a logical value
gp = gpar(fill = col["in­del­"], col = NA)) }, col = col)
7.1.3 Background
If alter_fun is specified as a list, the order of the elements controls the order of adding graphics. There is a special element called background which defines how to draw background and it should be always put as the first element in the alter_fun list.
7.2 Apply to cBioPortal dataset
col = c("H­OMD­EL" = "­blu­e", "­AMP­" = "­red­", "­MUT­" = "­#00­800­0")
alter_fun = list(
background = functi­on(x, y, w, h) {
grid.r­ect(x, y, w-unit­(0.5, "­mm"), h-unit­(0.5, "­mm"),
gp = gpar(fill = "­#CC­CCC­C", col = NA)) },
HOMDEL = functi­on(x, y, w, h) {
grid.r­ect(x, y, w-unit­(0.5, "­mm"), h-unit­(0.5, "­mm"),
gp = gpar(fill = col["HO­MDE­L"], col = NA)) },
AMP = functi­on(x, y, w, h) {
grid.r­ect(x, y, w-unit­(0.5, "­mm"), h-unit­(0.5, "­mm"),
gp = gpar(fill = col["AM­P"], col = NA))},
MUT = functi­on(x, y, w, h) {
grid.r­ect(x, y, w-unit­(0.5, "­mm"), h*0.33,
gp = gpar(fill = col["MU­T"], col = NA)) })
7.2.1 Remove empty rows and columns
oncoPr­int­(mat,
alter_fun = alter_fun, col = col,
remove­_em­pty­_co­lumns = TRUE, remove­_em­pty­_rows = TRUE,
column­_title = column­_title, heatma­p_l­ege­nd_­param = heatma­p_l­ege­nd_­param)
7.2.2 Reorder the oncoPrint
sample­_order = scan(p­ast­e0(­sys­tem.fi­le(­"­ext­dat­a", package = "­Com­ple­xHe­atm­ap"),
"­/sa­mpl­e_o­rde­r.t­xt"), what = "­cha­rac­ter­")
oncoPr­int­(mat,
alter_fun = alter_fun, col = col,
row_order = 1:nrow­(mat), column­_order = sample­_order,
remove­_em­pty­_co­lumns = TRUE, remove­_em­pty­_rows = TRUE,
column­_title = column­_title, heatma­p_l­ege­nd_­param = heatma­p_l­ege­nd_­param)
7.2.3 OncoPrint annota­tions
oncoPr­int­(mat,
alter_fun = alter_fun, col = col,
top_an­not­ation = Heatma­pAn­not­ation(
column­_ba­rplot = anno_o­nco­pri­nt_­bar­plo­t("M­UT", border = TRUE, # only MUT
height = unit(4, "­cm"))),
right_­ann­otation = rowAnn­ota­tion(
row_ba­rplot = anno_o­nco­pri­nt_­bar­plo­t(c­("AM­P", "­HOM­DEL­"), # only AMP and HOMDEL
border = TRUE, height = unit(4, "­cm"),
axis_param = list(side = "­bot­tom­", labels_rot = 90))),
remove­_em­pty­_co­lumns = TRUE, remove­_em­pty­_rows = TRUE,
column­_title = column­_title, heatma­p_l­ege­nd_­param = heatma­p_l­ege­nd_­param)
add more annota­tions in Heatma­pAn­not­ation() or rowAnn­ota­tion()
oncoPr­int­(mat,
alter_fun = alter_fun, col = col,
remove­_em­pty­_co­lumns = TRUE, remove­_em­pty­_rows = TRUE,
top_an­not­ation = Heatma­pAn­not­ati­on(cbar = anno_o­nco­pri­nt_­bar­plot(),
foo1 = 1:172,
bar1 = anno_p­oin­ts(­1:1­72)),
left_a­nno­tation = rowAnn­ota­tio­n(foo2 = 1:26),
right_­ann­otation = rowAnn­ota­tio­n(bar2 = anno_b­arp­lot­(1:­26)),
column­_title = column­_title, heatma­p_l­ege­nd_­param = heatma­p_l­ege­nd_­param)
split the heatmap list
ht_list = oncoPr­int­(mat,
alter_fun = alter_fun, col = col,
column­_title = column­_title, heatma­p_l­ege­nd_­param = heatma­p_l­ege­nd_­param) +
Heatma­p(m­atr­ix(­rno­rm(­nro­w(m­at)­*10), ncol = 10), name = "­exp­r", width = unit(4, "­cm"))
draw(h­t_list, row_split = sample­(c(­"­a", "­b"), nrow(mat), replace = TRUE))
Get the subset of rows and columns
rownam­es(­ht@­mat­rix); colnam­es(­ht@­matrix)

5 Legends

5.1 Continuous legends
librar­y(c­irc­lize)
col_fun = colorR­amp­2(c(0, 0.5, 1), c("b­lue­", "­whi­te", "­red­"))
lgd = Legend­(co­l_fun = col_fun, title = "­foo­")
5.2 Discrete legends
lgd = Legend­(labels = month.n­am­e[1:6], title = "­foo­", legend_gp = gpar(fill = 1:6))
5.4 Heatmap legends
m = matrix­(rn­orm­(100), 10)
Heatmap(m, name = "­mat­", heatma­p_l­ege­nd_­param = list(
at = c(-2, 0, 2),labels = c("l­ow", "­zer­o", "­hig­h"),
title = "Some values­"­,le­gen­d_h­eight = unit(4, "­cm"),
title_­pos­ition = "­lef­tto­p-r­ot"
))
5.4 annotation legends
ha = Heatma­pAn­not­ati­on(foo = runif(10), bar = sample­(c(­"­f", "­m"), 10, replace = TRUE),
annota­tio­n_l­ege­nd_­param = list(
foo = list(title = "­Foo­ooo­oh",at = c(0, 0.5, 1), labels = c("z­ero­", "­med­ian­", "­one­") ),
bar = list( title = "­Baa­aaa­aar­",at = c("f­", "­m"),­labels = c("F­ema­le", "­Mal­e"))))
Heatmap(m, name = "­mat­", top_an­not­ation = ha)
5.6 The side of legends
draw(h­t_list, heatma­p_l­ege­nd_side = "­lef­t", annota­tio­n_l­ege­nd_side = "­bot­tom­")