Table of Contents

Heatmap Decoration

Author: Zuguang Gu ( z.gu@dkfz.de )

Date: 2016-05-15


Each components of the heatmap/heatmap list has a name (unique id). You can go to any viewport to add graphics in by specifying the heatmap/annotation name.

First generate a figure that almost contains all types of heatmap components.

library(ComplexHeatmap)

mat = matrix(rnorm(80, 2), 8, 10)
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
rownames(mat) = paste0("R", 1:12)
colnames(mat) = paste0("C", 1:10)

ha_column1 = HeatmapAnnotation(points = anno_points(rnorm(10)))
ht1 = Heatmap(mat, name = "ht1", km = 2, row_title = "Heatmap 1", column_title = "Heatmap 1", 
    top_annotation = ha_column1)

ha_column2 = HeatmapAnnotation(df = data.frame(type = c(rep("a", 5), rep("b", 5))),
    col = list(type = c("a" = "red", "b" = "blue")))
ht2 = Heatmap(mat, name = "ht2", row_title = "Heatmap 2", column_title = "Heatmap 2",
    bottom_annotation = ha_column2)

plot of chunk access_components

The components (viewports) that have names are:

decorate_* functions

Basically, you can go to these components by seekViewport(), but to hide the details that is too low-level, ComplexHeatmap package provides decorate_* family functions which makes it easy to add graphics into different components.

Following code add annotation names, mark one grid in the heatmap and seperate the first column clusters with two rectangles.

ht_list = draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", 
    heatmap_legend_side = "right", annotation_legend_side = "left")
decorate_annotation("points", {
    grid.text("points", unit(0, "npc") - unit(2, "mm"), 0.5, 
        default.units = "npc", just = "right")
})

decorate_heatmap_body("ht1", {
    grid.text("outlier", 1.5/10, 2.5/4, default.units = "npc")
    grid.lines(c(0.5, 0.5), c(0, 1), gp = gpar(lty = 2, lwd = 2))
}, slice = 2)

decorate_column_dend("ht1", {
    tree = column_dend(ht_list)$ht1
    ind = cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)]

    first_index = function(l) which(l)[1]
    last_index = function(l) { x = which(l); x[length(x)] }
    x1 = c(first_index(ind == 1), first_index(ind == 2)) - 1
    x2 = c(last_index(ind == 1), last_index(ind == 2))
    grid.rect(x = x1/length(ind), width = (x2 - x1)/length(ind), just = "left",
        default.units = "npc", gp = gpar(fill = c("#FF000040", "#00FF0040"), col = NA))
})

decorate_row_names("ht2", {
    grid.rect(gp = gpar(fill = "#FF000040"))
}, slice = 2)

decorate_row_title("ht1", {
    grid.rect(gp = gpar(fill = "#00FF0040"))
}, slice = 1)

decorate_annotation("points", {
    grid.lines(c(0, 1), unit(c(0, 0), "native"), gp = gpar(col = "red"))
})

plot of chunk components

For annotations which are created by anno_points(), anno_barplot() and anno_boxplot(), “native” unit can be used in the decoration code.

Add annotation names

By default, annotation names are not plotted along with the heatmap annotations. The reason is if annotation names is plotted, they will located in the area of other heatmap components which would makes the adjustment of the heatmap layout difficult. But since you can go to any component in the heatmap list by its name, actually it is not difficult to add annotation names manually.

Following code add annotation names on the both sides of the column annotations. The drawback is since there is no specific component designed for annotation names, if the annotation name is too long, it will be exceeding the figure region (but this problem can be solved by some tricks, see the Examples vignette).

df = data.frame(type1 = c(rep("a", 5), rep("b", 5)),
                type2 = c(rep("A", 3), rep("B", 7)))
ha = HeatmapAnnotation(df, col = list(type1 = c("a" = "red", "b" = "blue"),
                                      type2 = c("A" = "green", "B" = "orange")))
Heatmap(mat, name = "ht", top_annotation = ha)
for(an in colnames(df)) {
    decorate_annotation(an, {
        # annotation names on the right
        grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left")
        # annotation names on the left
        grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right")
    })
}

plot of chunk annotation_title

Visualize distributions

With heatmap decorations, actually you can design new graphics based on heatmaps. Following is an example:

To visualize distribution of columns in a matrix or in a list, sometimes we use boxplot or beanplot. Here we can also use colors to map to the density values and visualize distribution of values in each column (or each list element) through a heatmap. Sometimes it can give you a more clear image of your data.

Here the package has a densityHeatmap() function, the usage is quite straightforward:

matrix = matrix(rnorm(100), 10); colnames(matrix) = letters[1:10]
ha = HeatmapAnnotation(df = data.frame(anno = rep(c("A", "B"), each = 5)),
    col = list(anno = c("A" = "green", "B" = "orange")),
    points = anno_points(runif(10)))
densityHeatmap(matrix, anno = ha)

plot of chunk density

Session info

sessionInfo()
## R version 3.3.0 (2016-05-03)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.4 LTS
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
##  [4] LC_COLLATE=C               LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
## [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
##  [1] stats4    parallel  grid      stats     graphics  grDevices utils     datasets  methods  
## [10] base     
## 
## other attached packages:
##  [1] GetoptLong_0.1.3      dendextend_1.1.8      dendsort_0.3.3        cluster_2.0.4        
##  [5] HilbertCurve_1.2.2    GenomicRanges_1.24.0  GenomeInfoDb_1.8.2    IRanges_2.6.0        
##  [9] S4Vectors_0.10.0      BiocGenerics_0.18.0   circlize_0.3.6        ComplexHeatmap_1.10.2
## [13] knitr_1.13            markdown_0.7.7       
## 
## loaded via a namespace (and not attached):
##  [1] whisker_0.3-2        XVector_0.12.0       magrittr_1.5         zlibbioc_1.18.0     
##  [5] lattice_0.20-33      colorspace_1.2-6     rjson_0.2.15         stringr_1.0.0       
##  [9] tools_3.3.0          png_0.1-7            RColorBrewer_1.1-2   formatR_1.4         
## [13] HilbertVis_1.30.0    GlobalOptions_0.0.10 shape_1.4.2          evaluate_0.9        
## [17] mime_0.4             stringi_1.0-1