1 | 1 |
deleted file mode 100755 |
... | ... |
@@ -1,239 +0,0 @@ |
1 |
-<!-- |
|
2 |
-%\VignetteEngine{knitr} |
|
3 |
-%\VignetteIndexEntry{5. Heatmap and Annotation Legends} |
|
4 |
- |
|
5 |
-Heatmap and Annotation Legends |
|
6 |
-======================================== |
|
7 |
- |
|
8 |
-**Author**: Zuguang Gu ( z.gu@dkfz.de ) |
|
9 |
- |
|
10 |
-**Date**: `r Sys.Date()` |
|
11 |
- |
|
12 |
- |
|
13 |
-```{r global_settings, echo = FALSE, message = FALSE} |
|
14 |
-library(markdown) |
|
15 |
-options(markdown.HTML.options = c(options('markdown.HTML.options')[[1]], "toc")) |
|
16 |
- |
|
17 |
-library(knitr) |
|
18 |
-knitr::opts_chunk$set( |
|
19 |
- error = FALSE, |
|
20 |
- tidy = FALSE, |
|
21 |
- message = FALSE, |
|
22 |
- fig.align = "center", |
|
23 |
- fig.width = 5, |
|
24 |
- fig.height = 5) |
|
25 |
-options(markdown.HTML.stylesheet = "custom.css") |
|
26 |
- |
|
27 |
-options(width = 100) |
|
28 |
-``` |
|
29 |
- |
|
30 |
-The legends for heatmaps are composed with a color bar, labels and titles. **ComplexHeatmap** automatically generates legends |
|
31 |
-according to the input matrix and annotations, while also provide flexibility to customize and add new legends. |
|
32 |
- |
|
33 |
- |
|
34 |
-## Basic settings |
|
35 |
- |
|
36 |
-Legends for all heatmaps and row annotations are drawn together and legends for all column annotations are drawn together. |
|
37 |
-The legends for heatmaps and legends for annotations are put in independent viewports. |
|
38 |
- |
|
39 |
-```{r legend_default, fig.width = 8, fig.keep = "all"} |
|
40 |
-library(ComplexHeatmap) |
|
41 |
-library(circlize) |
|
42 |
- |
|
43 |
-set.seed(123) |
|
44 |
-mat = matrix(rnorm(80, 2), 8, 10) |
|
45 |
-mat = rbind(mat, matrix(rnorm(40, -2), 4, 10)) |
|
46 |
-rownames(mat) = paste0("R", 1:12) |
|
47 |
-colnames(mat) = paste0("C", 1:10) |
|
48 |
- |
|
49 |
-ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))), |
|
50 |
- col = list(type1 = c("a" = "red", "b" = "blue"))) |
|
51 |
-ha_row = rowAnnotation(df = data.frame(type2 = c(rep("A", 6), rep("B", 6))), |
|
52 |
- col = list(type2 = c("A" = "green", "B" = "orange")), width = unit(1, "cm")) |
|
53 |
- |
|
54 |
-ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column) |
|
55 |
-ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2") |
|
56 |
-ht_list = ht1 + ht2 + ha_row |
|
57 |
- |
|
58 |
-draw(ht_list) |
|
59 |
-``` |
|
60 |
- |
|
61 |
-Side of legends can be set by `heatmap_legend_side` and `annotation_legend_side`. |
|
62 |
- |
|
63 |
-```{r legend_side, fig.width = 8, fig.keep = "all"} |
|
64 |
-draw(ht_list, heatmap_legend_side = "left", annotation_legend_side = "bottom") |
|
65 |
-``` |
|
66 |
- |
|
67 |
-`show_heatmap_legend` and `show_annotation_legend` set visibility of legends. |
|
68 |
- |
|
69 |
- |
|
70 |
-```{r legend_show, fig.width = 8, fig.keep = "all"} |
|
71 |
-draw(ht_list, show_heatmap_legend = FALSE, show_annotation_legend = FALSE) |
|
72 |
-``` |
|
73 |
- |
|
74 |
-You can choose to only show some of the heatmap legends by setting `show_heatmap_legend` to |
|
75 |
-a logical value when constructing single heatmaps. |
|
76 |
-Also `HeatmapAnnotation()` (or the shortcut function `columnAnnotation()` and `rowAnnotation()`) provides |
|
77 |
-`show_legend` argument to control visibility of annotation legends. |
|
78 |
- |
|
79 |
-```{r legend_show_part, fig.width = 10} |
|
80 |
-ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))), |
|
81 |
- col = list(type1 = c("a" = "red", "b" = "blue")), show_legend = FALSE) |
|
82 |
-ha_row = rowAnnotation(df = data.frame(type2 = c(rep("A", 6), rep("B", 6))), |
|
83 |
- col = list(type2 = c("A" = "green", "B" = "orange")), show_legend = FALSE, width = unit(1, "cm")) |
|
84 |
- |
|
85 |
-ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column) |
|
86 |
-ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", show_heatmap_legend = FALSE) |
|
87 |
-ht1 + ht2 + ha_row |
|
88 |
-``` |
|
89 |
- |
|
90 |
-## Customization of legends |
|
91 |
- |
|
92 |
-Legend itself can be flexibly customized. Parameters for making the legend can be set by `heatmap_legend_param` |
|
93 |
-(for heatmap) or `annotation_legend_param` (for annotations). |
|
94 |
-The parameters that can be set are as follows: |
|
95 |
- |
|
96 |
-- `title`: title of the legend |
|
97 |
-- `title_gp`: graphic parameters for the legend title |
|
98 |
-- `title_position`: position of title relative to the legend, possible values are `topcenter`, `topleft`, `leftcenter`, `lefttop`. |
|
99 |
-- `color_bar`: style of the color bar, i.e. continuous or discrete |
|
100 |
-- `grid_height`: height of the small grid in the color bar, only works for discrete color bar |
|
101 |
-- `grid_width`: width of the color bar |
|
102 |
-- `grid_border`: border of the color bar |
|
103 |
-- `at`: break values shown on the legend |
|
104 |
-- `labels`: labels which correspond to the breaks values |
|
105 |
-- `labels_gp`: graphic parameters for legend labels |
|
106 |
-- `nrow` and `ncol`: if there are too many legends, they can be put into an array. These two parameters controls number of rows or columns |
|
107 |
-- `legend_direction`: Controls the direction of the legend, possible values are `vertical` and `horizontal`. Works for both `continuous` and `discrete` |
|
108 |
-- `legend_width` and `legend_height`: when `color_bar` is `continuous`, the width or height of the legend |
|
109 |
- |
|
110 |
- |
|
111 |
-Following example changes the default graphic parameters for labels and titles: |
|
112 |
- |
|
113 |
-```{r heatmap_list_advanced, fig.width = 10} |
|
114 |
-df = data.frame(type = c(rep("a", 5), rep("b", 5))) |
|
115 |
-ha = HeatmapAnnotation(df = df, col = list(type = c("a" = "red", "b" = "blue")), |
|
116 |
- annotation_legend_param = list(type = list(title = "TYPE", title_gp = gpar(fontsize = 14), |
|
117 |
- labels_gp = gpar(fontsize = 8)))) |
|
118 |
-ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha) |
|
119 |
-ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", |
|
120 |
- heatmap_legend_param = list(title = "Heatmap2", title_gp = gpar(fontsize = 8), |
|
121 |
- labels_gp = gpar(fontsize = 14))) |
|
122 |
-ht1 + ht2 |
|
123 |
-``` |
|
124 |
- |
|
125 |
-You can specify break values and break labels (both for character values and numeric values) by `at` and `labels` |
|
126 |
-in corresponding `heatmap_legend_param` and `annotation_legend_param`. Note `at` can also be character break values. |
|
127 |
- |
|
128 |
-```{r self_define_heatmap_legend, fig.width = 10} |
|
129 |
-ha = HeatmapAnnotation(df = df, col = list(type = c("a" = "red", "b" = "blue")), |
|
130 |
- annotation_legend_param = list(type = list(title = "TYPE", title_gp = gpar(fontsize = 14), |
|
131 |
- labels_gp = gpar(fontsize = 8), at = c("a", "b"), labels = c("A", "B")))) |
|
132 |
-ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha, |
|
133 |
- heatmap_legend_param = list(at = c(-3, 0, 3), labels = c("-three", "zero", "+three"))) |
|
134 |
-ht1 + ht2 |
|
135 |
-``` |
|
136 |
- |
|
137 |
-If you have many levels in your annotation or matrix, you can put all levels into an array by specifying |
|
138 |
-`nrow` or `ncol`: |
|
139 |
- |
|
140 |
-```{r, fig.width = 6} |
|
141 |
-ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE), |
|
142 |
- annotation_legend_param = list(chr = list(ncol = 2, title = "chromosome", title_position = "topcenter")), |
|
143 |
- width = unit(5, "mm")) |
|
144 |
-ht1 = Heatmap(mat, name = "ht1") |
|
145 |
-ht1 + ha_chr |
|
146 |
-``` |
|
147 |
- |
|
148 |
-Or put at bottom of the heatmap: |
|
149 |
- |
|
150 |
-```{r, fig.width = 6} |
|
151 |
-ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE), |
|
152 |
- annotation_legend_param = list(chr = list(nrow = 2, title = "chr", title_position = "leftcenter")), |
|
153 |
- width = unit(5, "mm")) |
|
154 |
-ht1 = Heatmap(mat, name = "ht1", show_heatmap_legend = FALSE) |
|
155 |
-draw(ht1 + ha_chr, heatmap_legend_side = "bottom") |
|
156 |
-``` |
|
157 |
-If you want to order a discrete legend by column instead of row, use the direction argument: |
|
158 |
- |
|
159 |
-```{r, fig.width = 6} |
|
160 |
-ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE), |
|
161 |
- annotation_legend_param = list(chr = list(nrow = 2, title = "chr", title_position = "leftcenter", legend_direction = "vertical")), |
|
162 |
- width = unit(5, "mm")) |
|
163 |
-ht1 = Heatmap(mat, name = "ht1", show_heatmap_legend = FALSE) |
|
164 |
-draw(ht1 + ha_chr, heatmap_legend_side = "bottom") |
|
165 |
-``` |
|
166 |
- |
|
167 |
-Discrete color bar for can be used for continuous values, if you specify `color_bar` to `discrete`. |
|
168 |
-For the simple annotation which contains continuous values, `color_bar` can also be set to `discrete`. |
|
169 |
- |
|
170 |
-```{r} |
|
171 |
-ha = HeatmapAnnotation(df = data.frame(value = runif(10)), |
|
172 |
- col = list(value = colorRamp2(c(0, 1), c("white", "blue"))), |
|
173 |
- annotation_legend_param = list(color_bar = "discrete", at = c(0, 0.5, 1))) |
|
174 |
-Heatmap(mat, name = "ht1", top_annotation = ha, heatmap_legend_param = list(color_bar = "discrete")) |
|
175 |
-``` |
|
176 |
- |
|
177 |
-Some users prefer to put the legend at the bottom of heatmaps. |
|
178 |
- |
|
179 |
-```{r} |
|
180 |
-ht = Heatmap(mat, name = "ht1", heatmap_legend_param = list(legend_direction = "horizontal", |
|
181 |
- legend_width = unit(5, "cm"), title_position = "lefttop")) |
|
182 |
-draw(ht, heatmap_legend_side = "bottom") |
|
183 |
-``` |
|
184 |
- |
|
185 |
-Similarly, the height of the legend can be adjusted by `legend_height` if the legend is vertical. |
|
186 |
- |
|
187 |
-```{r} |
|
188 |
-Heatmap(mat, name = "ht1", heatmap_legend_param = list(legend_height = unit(5, "cm"))) |
|
189 |
-``` |
|
190 |
- |
|
191 |
-If you want to change default settings for all heatmaps/annotations, you can set it globally by `ht_global_opt()`. |
|
192 |
- |
|
193 |
-```{r, fig.width = 10} |
|
194 |
-ht_global_opt(heatmap_legend_title_gp = gpar(fontsize = 16), annotation_legend_labels_gp = gpar(fontface = "italic")) |
|
195 |
-ha = HeatmapAnnotation(df = data.frame(value = runif(10)), |
|
196 |
- col = list(value = colorRamp2(c(0, 1), c("white", "blue")))) |
|
197 |
-ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha) |
|
198 |
-ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", heatmap_legend_param = list(title_gp = gpar(fontsize = 8))) |
|
199 |
-ht1 + ht2 |
|
200 |
-ht_global_opt(RESET = TRUE) |
|
201 |
-``` |
|
202 |
- |
|
203 |
-## Add new legends |
|
204 |
- |
|
205 |
-**ComplexHeatmap** only generates legends for heatmaps and simple annotations. Self-defined legends |
|
206 |
-can be passed by `heatmap_legend_list` and `annotation_legend_list` as a list of `grob` objects. |
|
207 |
- |
|
208 |
-**grid** package provides `legendGrob()` to construct a legend grob with certain style but |
|
209 |
-styles are still limited. For advanced users, they can construct a legend grob totally from ground by `frameGrob()` |
|
210 |
-and `placeGrob()`. |
|
211 |
- |
|
212 |
-```{r self_defined_annotation_legend, fig.width = 10} |
|
213 |
-ha = HeatmapAnnotation(points = anno_points(rnorm(10))) |
|
214 |
-ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", top_annotation = ha, show_heatmap_legend = FALSE) |
|
215 |
-lgd = legendGrob(c("dots"), pch = 16) |
|
216 |
-draw(ht1 + ht2, annotation_legend_list = list(lgd)) |
|
217 |
-``` |
|
218 |
- |
|
219 |
-From version 1.9.7, **ComplexHeatmap** package provides a `Legend()` function which can produce legends in `grob` formats |
|
220 |
-(actually all legends in the package are implemented by `Legend()` function). |
|
221 |
-In following example, we have several column annotations which contains points and we also want to show legends |
|
222 |
-for these non-heatmap graphics. |
|
223 |
- |
|
224 |
-```{r} |
|
225 |
-ha = HeatmapAnnotation(points = anno_points(rnorm(10), gp = gpar(col = rep(2:3, each = 5)))) |
|
226 |
-ht = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", top_annotation = ha) |
|
227 |
-lgd = Legend(at = c("class1", "class2"), title = "points", type = "points", legend_gp = gpar(col = 2:3)) |
|
228 |
-draw(ht, annotation_legend_list = list(lgd)) |
|
229 |
-``` |
|
230 |
- |
|
231 |
-Also check [this blog link](http://zuguang.de/blog/html/dde69a9cf5a606a9486b19eb91ba6f4e.html) for more demonstrations. |
|
232 |
- |
|
233 |
-## Session info |
|
234 |
- |
|
235 |
-```{r} |
|
236 |
-sessionInfo() |
|
237 |
-``` |
1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,239 @@ |
1 |
+<!-- |
|
2 |
+%\VignetteEngine{knitr} |
|
3 |
+%\VignetteIndexEntry{5. Heatmap and Annotation Legends} |
|
4 |
+--> |
|
5 |
+ |
|
6 |
+Heatmap and Annotation Legends |
|
7 |
+======================================== |
|
8 |
+ |
|
9 |
+**Author**: Zuguang Gu ( z.gu@dkfz.de ) |
|
10 |
+ |
|
11 |
+**Date**: `r Sys.Date()` |
|
12 |
+ |
|
13 |
+------------------------------------------------------------- |
|
14 |
+ |
|
15 |
+```{r global_settings, echo = FALSE, message = FALSE} |
|
16 |
+library(markdown) |
|
17 |
+options(markdown.HTML.options = c(options('markdown.HTML.options')[[1]], "toc")) |
|
18 |
+ |
|
19 |
+library(knitr) |
|
20 |
+knitr::opts_chunk$set( |
|
21 |
+ error = FALSE, |
|
22 |
+ tidy = FALSE, |
|
23 |
+ message = FALSE, |
|
24 |
+ fig.align = "center", |
|
25 |
+ fig.width = 5, |
|
26 |
+ fig.height = 5) |
|
27 |
+options(markdown.HTML.stylesheet = "custom.css") |
|
28 |
+ |
|
29 |
+options(width = 100) |
|
30 |
+``` |
|
31 |
+ |
|
32 |
+The legends for heatmaps are composed with a color bar, labels and titles. **ComplexHeatmap** automatically generates legends |
|
33 |
+according to the input matrix and annotations, while also provide flexibility to customize and add new legends. |
|
34 |
+ |
|
35 |
+ |
|
36 |
+## Basic settings |
|
37 |
+ |
|
38 |
+Legends for all heatmaps and row annotations are drawn together and legends for all column annotations are drawn together. |
|
39 |
+The legends for heatmaps and legends for annotations are put in independent viewports. |
|
40 |
+ |
|
41 |
+```{r legend_default, fig.width = 8, fig.keep = "all"} |
|
42 |
+library(ComplexHeatmap) |
|
43 |
+library(circlize) |
|
44 |
+ |
|
45 |
+set.seed(123) |
|
46 |
+mat = matrix(rnorm(80, 2), 8, 10) |
|
47 |
+mat = rbind(mat, matrix(rnorm(40, -2), 4, 10)) |
|
48 |
+rownames(mat) = paste0("R", 1:12) |
|
49 |
+colnames(mat) = paste0("C", 1:10) |
|
50 |
+ |
|
51 |
+ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))), |
|
52 |
+ col = list(type1 = c("a" = "red", "b" = "blue"))) |
|
53 |
+ha_row = rowAnnotation(df = data.frame(type2 = c(rep("A", 6), rep("B", 6))), |
|
54 |
+ col = list(type2 = c("A" = "green", "B" = "orange")), width = unit(1, "cm")) |
|
55 |
+ |
|
56 |
+ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column) |
|
57 |
+ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2") |
|
58 |
+ht_list = ht1 + ht2 + ha_row |
|
59 |
+ |
|
60 |
+draw(ht_list) |
|
61 |
+``` |
|
62 |
+ |
|
63 |
+Side of legends can be set by `heatmap_legend_side` and `annotation_legend_side`. |
|
64 |
+ |
|
65 |
+```{r legend_side, fig.width = 8, fig.keep = "all"} |
|
66 |
+draw(ht_list, heatmap_legend_side = "left", annotation_legend_side = "bottom") |
|
67 |
+``` |
|
68 |
+ |
|
69 |
+`show_heatmap_legend` and `show_annotation_legend` set visibility of legends. |
|
70 |
+ |
|
71 |
+ |
|
72 |
+```{r legend_show, fig.width = 8, fig.keep = "all"} |
|
73 |
+draw(ht_list, show_heatmap_legend = FALSE, show_annotation_legend = FALSE) |
|
74 |
+``` |
|
75 |
+ |
|
76 |
+You can choose to only show some of the heatmap legends by setting `show_heatmap_legend` to |
|
77 |
+a logical value when constructing single heatmaps. |
|
78 |
+Also `HeatmapAnnotation()` (or the shortcut function `columnAnnotation()` and `rowAnnotation()`) provides |
|
79 |
+`show_legend` argument to control visibility of annotation legends. |
|
80 |
+ |
|
81 |
+```{r legend_show_part, fig.width = 10} |
|
82 |
+ha_column = HeatmapAnnotation(df = data.frame(type1 = c(rep("a", 5), rep("b", 5))), |
|
83 |
+ col = list(type1 = c("a" = "red", "b" = "blue")), show_legend = FALSE) |
|
84 |
+ha_row = rowAnnotation(df = data.frame(type2 = c(rep("A", 6), rep("B", 6))), |
|
85 |
+ col = list(type2 = c("A" = "green", "B" = "orange")), show_legend = FALSE, width = unit(1, "cm")) |
|
86 |
+ |
|
87 |
+ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha_column) |
|
88 |
+ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", show_heatmap_legend = FALSE) |
|
89 |
+ht1 + ht2 + ha_row |
|
90 |
+``` |
|
91 |
+ |
|
92 |
+## Customization of legends |
|
93 |
+ |
|
94 |
+Legend itself can be flexibly customized. Parameters for making the legend can be set by `heatmap_legend_param` |
|
95 |
+(for heatmap) or `annotation_legend_param` (for annotations). |
|
96 |
+The parameters that can be set are as follows: |
|
97 |
+ |
|
98 |
+- `title`: title of the legend |
|
99 |
+- `title_gp`: graphic parameters for the legend title |
|
100 |
+- `title_position`: position of title relative to the legend, possible values are `topcenter`, `topleft`, `leftcenter`, `lefttop`. |
|
101 |
+- `color_bar`: style of the color bar, i.e. continuous or discrete |
|
102 |
+- `grid_height`: height of the small grid in the color bar, only works for discrete color bar |
|
103 |
+- `grid_width`: width of the color bar |
|
104 |
+- `grid_border`: border of the color bar |
|
105 |
+- `at`: break values shown on the legend |
|
106 |
+- `labels`: labels which correspond to the breaks values |
|
107 |
+- `labels_gp`: graphic parameters for legend labels |
|
108 |
+- `nrow` and `ncol`: if there are too many legends, they can be put into an array. These two parameters controls number of rows or columns |
|
109 |
+- `legend_direction`: Controls the direction of the legend, possible values are `vertical` and `horizontal`. Works for both `continuous` and `discrete` |
|
110 |
+- `legend_width` and `legend_height`: when `color_bar` is `continuous`, the width or height of the legend |
|
111 |
+ |
|
112 |
+ |
|
113 |
+Following example changes the default graphic parameters for labels and titles: |
|
114 |
+ |
|
115 |
+```{r heatmap_list_advanced, fig.width = 10} |
|
116 |
+df = data.frame(type = c(rep("a", 5), rep("b", 5))) |
|
117 |
+ha = HeatmapAnnotation(df = df, col = list(type = c("a" = "red", "b" = "blue")), |
|
118 |
+ annotation_legend_param = list(type = list(title = "TYPE", title_gp = gpar(fontsize = 14), |
|
119 |
+ labels_gp = gpar(fontsize = 8)))) |
|
120 |
+ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha) |
|
121 |
+ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", |
|
122 |
+ heatmap_legend_param = list(title = "Heatmap2", title_gp = gpar(fontsize = 8), |
|
123 |
+ labels_gp = gpar(fontsize = 14))) |
|
124 |
+ht1 + ht2 |
|
125 |
+``` |
|
126 |
+ |
|
127 |
+You can specify break values and break labels (both for character values and numeric values) by `at` and `labels` |
|
128 |
+in corresponding `heatmap_legend_param` and `annotation_legend_param`. Note `at` can also be character break values. |
|
129 |
+ |
|
130 |
+```{r self_define_heatmap_legend, fig.width = 10} |
|
131 |
+ha = HeatmapAnnotation(df = df, col = list(type = c("a" = "red", "b" = "blue")), |
|
132 |
+ annotation_legend_param = list(type = list(title = "TYPE", title_gp = gpar(fontsize = 14), |
|
133 |
+ labels_gp = gpar(fontsize = 8), at = c("a", "b"), labels = c("A", "B")))) |
|
134 |
+ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha, |
|
135 |
+ heatmap_legend_param = list(at = c(-3, 0, 3), labels = c("-three", "zero", "+three"))) |
|
136 |
+ht1 + ht2 |
|
137 |
+``` |
|
138 |
+ |
|
139 |
+If you have many levels in your annotation or matrix, you can put all levels into an array by specifying |
|
140 |
+`nrow` or `ncol`: |
|
141 |
+ |
|
142 |
+```{r, fig.width = 6} |
|
143 |
+ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE), |
|
144 |
+ annotation_legend_param = list(chr = list(ncol = 2, title = "chromosome", title_position = "topcenter")), |
|
145 |
+ width = unit(5, "mm")) |
|
146 |
+ht1 = Heatmap(mat, name = "ht1") |
|
147 |
+ht1 + ha_chr |
|
148 |
+``` |
|
149 |
+ |
|
150 |
+Or put at bottom of the heatmap: |
|
151 |
+ |
|
152 |
+```{r, fig.width = 6} |
|
153 |
+ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE), |
|
154 |
+ annotation_legend_param = list(chr = list(nrow = 2, title = "chr", title_position = "leftcenter")), |
|
155 |
+ width = unit(5, "mm")) |
|
156 |
+ht1 = Heatmap(mat, name = "ht1", show_heatmap_legend = FALSE) |
|
157 |
+draw(ht1 + ha_chr, heatmap_legend_side = "bottom") |
|
158 |
+``` |
|
159 |
+If you want to order a discrete legend by column instead of row, use the direction argument: |
|
160 |
+ |
|
161 |
+```{r, fig.width = 6} |
|
162 |
+ha_chr = rowAnnotation(chr = sample(paste0("chr", 1:20), nrow(mat), replace = TRUE), |
|
163 |
+ annotation_legend_param = list(chr = list(nrow = 2, title = "chr", title_position = "leftcenter", legend_direction = "vertical")), |
|
164 |
+ width = unit(5, "mm")) |
|
165 |
+ht1 = Heatmap(mat, name = "ht1", show_heatmap_legend = FALSE) |
|
166 |
+draw(ht1 + ha_chr, heatmap_legend_side = "bottom") |
|
167 |
+``` |
|
168 |
+ |
|
169 |
+Discrete color bar for can be used for continuous values, if you specify `color_bar` to `discrete`. |
|
170 |
+For the simple annotation which contains continuous values, `color_bar` can also be set to `discrete`. |
|
171 |
+ |
|
172 |
+```{r} |
|
173 |
+ha = HeatmapAnnotation(df = data.frame(value = runif(10)), |
|
174 |
+ col = list(value = colorRamp2(c(0, 1), c("white", "blue"))), |
|
175 |
+ annotation_legend_param = list(color_bar = "discrete", at = c(0, 0.5, 1))) |
|
176 |
+Heatmap(mat, name = "ht1", top_annotation = ha, heatmap_legend_param = list(color_bar = "discrete")) |
|
177 |
+``` |
|
178 |
+ |
|
179 |
+Some users prefer to put the legend at the bottom of heatmaps. |
|
180 |
+ |
|
181 |
+```{r} |
|
182 |
+ht = Heatmap(mat, name = "ht1", heatmap_legend_param = list(legend_direction = "horizontal", |
|
183 |
+ legend_width = unit(5, "cm"), title_position = "lefttop")) |
|
184 |
+draw(ht, heatmap_legend_side = "bottom") |
|
185 |
+``` |
|
186 |
+ |
|
187 |
+Similarly, the height of the legend can be adjusted by `legend_height` if the legend is vertical. |
|
188 |
+ |
|
189 |
+```{r} |
|
190 |
+Heatmap(mat, name = "ht1", heatmap_legend_param = list(legend_height = unit(5, "cm"))) |
|
191 |
+``` |
|
192 |
+ |
|
193 |
+If you want to change default settings for all heatmaps/annotations, you can set it globally by `ht_global_opt()`. |
|
194 |
+ |
|
195 |
+```{r, fig.width = 10} |
|
196 |
+ht_global_opt(heatmap_legend_title_gp = gpar(fontsize = 16), annotation_legend_labels_gp = gpar(fontface = "italic")) |
|
197 |
+ha = HeatmapAnnotation(df = data.frame(value = runif(10)), |
|
198 |
+ col = list(value = colorRamp2(c(0, 1), c("white", "blue")))) |
|
199 |
+ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", top_annotation = ha) |
|
200 |
+ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", heatmap_legend_param = list(title_gp = gpar(fontsize = 8))) |
|
201 |
+ht1 + ht2 |
|
202 |
+ht_global_opt(RESET = TRUE) |
|
203 |
+``` |
|
204 |
+ |
|
205 |
+## Add new legends |
|
206 |
+ |
|
207 |
+**ComplexHeatmap** only generates legends for heatmaps and simple annotations. Self-defined legends |
|
208 |
+can be passed by `heatmap_legend_list` and `annotation_legend_list` as a list of `grob` objects. |
|
209 |
+ |
|
210 |
+**grid** package provides `legendGrob()` to construct a legend grob with certain style but |
|
211 |
+styles are still limited. For advanced users, they can construct a legend grob totally from ground by `frameGrob()` |
|
212 |
+and `placeGrob()`. |
|
213 |
+ |
|
214 |
+```{r self_defined_annotation_legend, fig.width = 10} |
|
215 |
+ha = HeatmapAnnotation(points = anno_points(rnorm(10))) |
|
216 |
+ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", top_annotation = ha, show_heatmap_legend = FALSE) |
|
217 |
+lgd = legendGrob(c("dots"), pch = 16) |
|
218 |
+draw(ht1 + ht2, annotation_legend_list = list(lgd)) |
|
219 |
+``` |
|
220 |
+ |
|
221 |
+From version 1.9.7, **ComplexHeatmap** package provides a `Legend()` function which can produce legends in `grob` formats |
|
222 |
+(actually all legends in the package are implemented by `Legend()` function). |
|
223 |
+In following example, we have several column annotations which contains points and we also want to show legends |
|
224 |
+for these non-heatmap graphics. |
|
225 |
+ |
|
226 |
+```{r} |
|
227 |
+ha = HeatmapAnnotation(points = anno_points(rnorm(10), gp = gpar(col = rep(2:3, each = 5)))) |
|
228 |
+ht = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", top_annotation = ha) |
|
229 |
+lgd = Legend(at = c("class1", "class2"), title = "points", type = "points", legend_gp = gpar(col = 2:3)) |
|
230 |
+draw(ht, annotation_legend_list = list(lgd)) |
|
231 |
+``` |
|
232 |
+ |
|
233 |
+Also check [this blog link](http://zuguang.de/blog/html/dde69a9cf5a606a9486b19eb91ba6f4e.html) for more demonstrations. |
|
234 |
+ |
|
235 |
+## Session info |
|
236 |
+ |
|
237 |
+```{r} |
|
238 |
+sessionInfo() |
|
239 |
+``` |