Home | Blog | Software | Publications | GitHub

Add column names below bottom annotations in the heatmap

In the heamtap generated by ComplexHeatmap package, column names are drawn just below the heatmap body. If column annotations are put at the bottom of the heatmap as well, the annotations will be put below the column names. See following example:

## Loading required package: grid
## Warning in .doLoadActions(where, attach): trying to execute load actions
## without 'methods' package
## Warning: replacing previous import by 'magrittr::%>%' when loading
## 'dendextend'
mat = matrix(rnorm(100), 10)
foo = sample(letters[1:2], 10, replace = TRUE)
colnames(mat) = sapply(letters[1:10], function(x) paste(rep(x, 10), collapse = ""))
ha = HeatmapAnnotation(foo = foo)
Heatmap(mat, bottom_annotation = ha)

plot of chunk unnamed-chunk-1

In order to put the column names below bottom annotations, you can do as follows:

  1. suppress column names
  2. add a text annotation which contains column names and put it as the last bottom annotation.

To correctly calcualte the height of column names, you need to use max_text_width() function.

cn = colnames(mat)
ha2 = HeatmapAnnotation(foo = foo,
    cn = anno_text(cn, rot = 90, offset = unit(1, "npc") - unit(2, "mm"), just = "right"),
    annotation_height = unit.c(unit(5, "mm"), max_text_width(cn)))
Heatmap(mat, show_column_names = FALSE, bottom_annotation = ha2)

plot of chunk unnamed-chunk-2