Home | Blog | Software | Publications | GitHub
In ComplexHeatmap package, legends for heatmap and annotations are differently treated and they will be put in different places. However, if there are not too many heatmaps and annotations, separating these two types of legends will be kind of waste of spaces.
draw() method of
HeatmapList class, there is an option
annotation_legend_list) which accepts
additional self-defined legends (in a form of
grob objects). Thus, one strategy to merge
heatmap legends and annotation legends is to extract the legends first and suppress plotting legends,
later we construct a list of legends and send to
In following example, there is only one heatmap and two simple column annotations.
When constructing column annotaiton
show_legend is set to
FALSE to suppress annotation legends.
library(ComplexHeatmap) library(circlize) df = data.frame(type = c(rep("a", 5), rep("b", 5)), age = sample(1:20, 10)) ha = HeatmapAnnotation(df = df, col = list(type = c("a" = "red", "b" = "blue"), age = colorRamp2(c(0, 20), c("white", "red"))), show_legend = FALSE )
Color mapping information is stored in
ha and we can extract legends from
anno_legend_list = lapply(ha@anno_list[c("type", "age")], function(anno) color_mapping_legend(anno@color_mapping, plot = FALSE)) anno_legend_list
## $type ## frame[GRID.frame.559] ## ## $age ## frame[GRID.frame.574]
show_heatmap_legend is set to
FALSE when creating
ht and legend object is extracted afterwards.
mat = matrix(rnorm(80, 2), 8, 10) ht = Heatmap(mat, name = "ht", top_annotation = ha, show_heatmap_legend = FALSE) heatmap_legend = color_mapping_legend(ht@matrix_color_mapping, plot = FALSE)
draw() is called explicitely and a list of both heatmap legend and annotation legends are sent to
draw(ht, heatmap_legend_list = c(anno_legend_list, list(heatmap_legend)))