Home | Blog | Software | Publications | GitHub


ComplexHeatmap Supports More Types of Legends

From version 1.9.7, ComplexHeatmap package supports more types of legends. Basic improvements are:

  1. position of title can be adjusted. There are now four optinos: topleft, topcenter, leftcenter and lefttop
  2. when there are too many levels for a legend, they can be arranged as an array
  3. height or width of continuous color bar can be adjusted
  4. the continuous color bar can be set as vertical or horizontal
  5. legends of points or lines can also be generated.

First let's generate a random matrix:

set.seed(123)
mat = matrix(rnorm(100), 10)

Now we generate legends with different styles:

library(ComplexHeatmap)
Heatmap(mat, name = "fooooo")

plot of chunk unnamed-chunk-2

Heatmap(mat, name = "fooooo", heatmap_legend_param = list(title_position = "topcenter"))

plot of chunk unnamed-chunk-2

Heatmap(mat, name = "fooooo", heatmap_legend_param = list(color_bar = "continuous"))

plot of chunk unnamed-chunk-2

Heatmap(mat, name = "fooooo", heatmap_legend_param = list(color_bar = "continuous", 
    legend_height = unit(5, "cm")))

plot of chunk unnamed-chunk-2

Heatmap(mat, name = "fooooo", heatmap_legend_param = list(color_bar = "continuous", 
    at = quantile(mat, c(0, 0.5, 1)), labels = c("low", "median", "high"),
    legend_height = unit(5, "cm")))

plot of chunk unnamed-chunk-2

Next we put the legend at the bottom of the heatmap:

ht = Heatmap(mat, name = "fooooo")
draw(ht, heatmap_legend_side = "bottom")

plot of chunk unnamed-chunk-3

ht = Heatmap(mat, name = "fooooo", heatmap_legend_param = list(title_position = "topcenter",
    color_bar = "continuous", legend_direction = "horizontal"))
draw(ht, heatmap_legend_side = "bottom")

plot of chunk unnamed-chunk-3

ht = Heatmap(mat, name = "fooooo", heatmap_legend_param = list(title_position = "lefttop",
    color_bar = "continuous", legend_direction = "horizontal"))
draw(ht, heatmap_legend_side = "bottom")

plot of chunk unnamed-chunk-3

ht = Heatmap(mat, name = "fooooo", heatmap_legend_param = list(title_position = "topcenter",
    color_bar = "continuous", legend_direction = "horizontal"))
draw(ht, heatmap_legend_side = "bottom")

plot of chunk unnamed-chunk-3

ht = Heatmap(mat, name = "fooooo", heatmap_legend_param = list(title_position = "topcenter",
    title = "bar", color_bar = "continuous", legend_direction = "horizontal", 
    at = quantile(mat, c(0, 0.5, 1)), labels = c("low", "median", "high"), 
    legend_width = unit(5, "cm")))
draw(ht, heatmap_legend_side = "bottom")

plot of chunk unnamed-chunk-3

If we have a character matrix:

cmat = matrix(sample(letters[1:12], 100, replace = TRUE), 10)
colors = structure(circlize::rand_color(12), names = letters[1:12])
Heatmap(cmat, name = "fooooo", col = colors)

plot of chunk unnamed-chunk-4

Heatmap(cmat, name = "fooooo", col = colors, heatmap_legend_param = list(ncol = 2))

plot of chunk unnamed-chunk-4

Heatmap(cmat, name = "fooooo", col = colors, heatmap_legend_param = list(title_position = "topcenter", 
    nrow = 4))

plot of chunk unnamed-chunk-4

ht = Heatmap(cmat, name = "fooooo", col = colors, heatmap_legend_param = list(title_position = "topcenter", 
    nrow = 2))
draw(ht, heatmap_legend_side = "bottom")

plot of chunk unnamed-chunk-4

ht = Heatmap(cmat, name = "fooooo", col = colors, heatmap_legend_param = list(title_position = "leftcenter", 
    nrow = 1))
draw(ht, heatmap_legend_side = "bottom")

plot of chunk unnamed-chunk-4

The same rules apply to the annotation legends.

ComplexHeatmap only automatically generates legends for heatmaps and annotations, however, the new added function Legend() also generate a grob object which contains more general legends, e.g. points or lines. These legends can be used for self-defined annotations.

ha = HeatmapAnnotation(pt = anno_points(runif(10), gp = gpar(col = rep(2:3, 5))))
ht = Heatmap(mat, name = "fooooo", top_annotation = ha)
lgd = Legend(at = c("class1", "class2"), type = "points", legend_gp = gpar(col = 2:3), title = "Class")
draw(ht, annotation_legend_list = list(lgd))

plot of chunk unnamed-chunk-5