1 | 1 |
deleted file mode 100755 |
... | ... |
@@ -1,216 +0,0 @@ |
1 |
-<!-- |
|
2 |
-%\VignetteEngine{knitr} |
|
3 |
-%\VignetteIndexEntry{6. Heatmap Decoration} |
|
4 |
- |
|
5 |
-Heatmap Decoration |
|
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 |
-Each components of the heatmap/heatmap list has a name (unique id). You can go to any viewport |
|
31 |
-to add graphics in by specifying the heatmap/annotation name. |
|
32 |
- |
|
33 |
-First generate a figure that almost contains all types of heatmap components. |
|
34 |
- |
|
35 |
-```{r access_components, fig.width = 10, fig.height = 7, echo = 1:13} |
|
36 |
-library(ComplexHeatmap) |
|
37 |
- |
|
38 |
-mat = matrix(rnorm(80, 2), 8, 10) |
|
39 |
-mat = rbind(mat, matrix(rnorm(40, -2), 4, 10)) |
|
40 |
-rownames(mat) = paste0("R", 1:12) |
|
41 |
-colnames(mat) = paste0("C", 1:10) |
|
42 |
- |
|
43 |
-ha_column1 = HeatmapAnnotation(points = anno_points(rnorm(10))) |
|
44 |
-ht1 = Heatmap(mat, name = "ht1", km = 2, row_title = "Heatmap 1", column_title = "Heatmap 1", |
|
45 |
- top_annotation = ha_column1) |
|
46 |
- |
|
47 |
-ha_column2 = HeatmapAnnotation(df = data.frame(type = c(rep("a", 5), rep("b", 5))), |
|
48 |
- col = list(type = c("a" = "red", "b" = "blue"))) |
|
49 |
-ht2 = Heatmap(mat, name = "ht2", row_title = "Heatmap 2", column_title = "Heatmap 2", |
|
50 |
- bottom_annotation = ha_column2) |
|
51 |
- |
|
52 |
-ht_list = ht1 + ht2 + rowAnnotation(bar = row_anno_barplot(rowMeans(mat))) |
|
53 |
-draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", |
|
54 |
- heatmap_legend_side = "right", annotation_legend_side = "left") |
|
55 |
- |
|
56 |
-library(GetoptLong) |
|
57 |
-seekViewport("global"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
58 |
-seekViewport("global_column_title"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
59 |
-seekViewport("global_row_title"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
60 |
-seekViewport("main_heatmap_list"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
61 |
-for(heatmap_name in c("ht1", "ht2")) { |
|
62 |
- seekViewport(qq("heatmap_@{heatmap_name}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
63 |
- for(i in 1:2) { |
|
64 |
- seekViewport(qq("@{heatmap_name}_heatmap_body_@{i}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
65 |
- seekViewport(qq("@{heatmap_name}_row_names_@{i}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
66 |
- } |
|
67 |
- |
|
68 |
- seekViewport(qq("@{heatmap_name}_column_title")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
69 |
- seekViewport(qq("@{heatmap_name}_dend_column")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
70 |
- seekViewport(qq("@{heatmap_name}_column_names")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
71 |
- seekViewport(qq("legend_@{heatmap_name}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
72 |
-} |
|
73 |
-seekViewport(qq("ht1_dend_row_2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
74 |
-seekViewport(qq("ht1_dend_row_1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
75 |
-seekViewport(qq("ht1_row_title_1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
76 |
-seekViewport(qq("ht1_row_title_2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
77 |
- |
|
78 |
- |
|
79 |
-for(annotation_name in c("points", "type")) { |
|
80 |
- seekViewport(qq("annotation_@{annotation_name}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
81 |
-} |
|
82 |
-seekViewport(qq("annotation_bar_1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
83 |
-seekViewport(qq("annotation_bar_2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
84 |
- |
|
85 |
-seekViewport(qq("legend_type")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
86 |
-seekViewport(qq("legend_ht1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
87 |
-seekViewport(qq("legend_ht2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
88 |
- |
|
89 |
-seekViewport("heatmap_legend"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
90 |
-seekViewport("annotation_legend"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
91 |
-``` |
|
92 |
- |
|
93 |
-The components (viewports) that have names are: |
|
94 |
- |
|
95 |
-- `global`: the viewport which contains the whole figure. |
|
96 |
-- `global_column_title`: the viewport which contains column title for the heatmap list. |
|
97 |
-- `global_row_title`: the viewport which contains row title for the heatmap list. |
|
98 |
-- `main_heatmap_list`: the viewport which contains a list of heatmaps and row annotations. |
|
99 |
-- `heatmap_@{heatmap_name}`: the viewport which contains a single heatmap |
|
100 |
-- `annotation_@{annotation_name}`: the viewport which contains an annotation on columns. |
|
101 |
-- `annotation_@{annotation_name}_@{i}`: for row annotations |
|
102 |
-- `@{heatmap_name}_heatmap_body_@{i}`: the heatmap body. |
|
103 |
-- `@{heatmap_name}_column_title`: column title for a single heatmap. |
|
104 |
-- `@{heatmap_name}_row_title_@{i}`: since a heatmap body may be splitted into several parts. `@{i}` is the index of the row slice. |
|
105 |
-- `@{heatmap_name}_dend_row_@{i}`: dendrogram for ith row slice. |
|
106 |
-- `@{heatmap_name}_dend_column`: dendrogram on columns |
|
107 |
-- `@{heatmap_name}_row_names_@{i}`: the viewport which contains row names. |
|
108 |
-- `@{heatmap_name}_column_names`: the viewport which contains column names. |
|
109 |
-- `heatmap_legend`: the viewport which contains all heatmap legends. |
|
110 |
-- `legend_@{heatmap_name}`: the viewport which contains a single heatmap legend. |
|
111 |
-- `annotation_legend`: the viewport which contains all annotation legends. |
|
112 |
-- `legend_@{annotation_name}`: the viewport which contains a single annotation legend. |
|
113 |
- |
|
114 |
-## decorate_* functions |
|
115 |
- |
|
116 |
-Basically, you can go to these components by `seekViewport()`, but to hide the details that is too low-level, |
|
117 |
-**ComplexHeatmap** package provides `decorate_*` family functions which makes it easy to add graphics into different components. |
|
118 |
- |
|
119 |
-Following code add annotation names, mark one grid in the heatmap and seperate the first column clusters with two rectangles. |
|
120 |
- |
|
121 |
-```{r components, fig.width = 10, fig.height = 7} |
|
122 |
-ht_list = draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", |
|
123 |
- heatmap_legend_side = "right", annotation_legend_side = "left") |
|
124 |
-decorate_annotation("points", { |
|
125 |
- grid.text("points", unit(0, "npc") - unit(2, "mm"), 0.5, |
|
126 |
- default.units = "npc", just = "right") |
|
127 |
-}) |
|
128 |
- |
|
129 |
-decorate_heatmap_body("ht1", { |
|
130 |
- grid.text("outlier", 1.5/10, 2.5/4, default.units = "npc") |
|
131 |
- grid.lines(c(0.5, 0.5), c(0, 1), gp = gpar(lty = 2, lwd = 2)) |
|
132 |
-}, slice = 2) |
|
133 |
- |
|
134 |
-decorate_column_dend("ht1", { |
|
135 |
- tree = column_dend(ht_list)$ht1 |
|
136 |
- ind = cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)] |
|
137 |
- |
|
138 |
- first_index = function(l) which(l)[1] |
|
139 |
- last_index = function(l) { x = which(l); x[length(x)] } |
|
140 |
- x1 = c(first_index(ind == 1), first_index(ind == 2)) - 1 |
|
141 |
- x2 = c(last_index(ind == 1), last_index(ind == 2)) |
|
142 |
- grid.rect(x = x1/length(ind), width = (x2 - x1)/length(ind), just = "left", |
|
143 |
- default.units = "npc", gp = gpar(fill = c("#FF000040", "#00FF0040"), col = NA)) |
|
144 |
-}) |
|
145 |
- |
|
146 |
-decorate_row_names("ht2", { |
|
147 |
- grid.rect(gp = gpar(fill = "#FF000040")) |
|
148 |
-}, slice = 2) |
|
149 |
- |
|
150 |
-decorate_row_title("ht1", { |
|
151 |
- grid.rect(gp = gpar(fill = "#00FF0040")) |
|
152 |
-}, slice = 1) |
|
153 |
- |
|
154 |
-decorate_annotation("points", { |
|
155 |
- grid.lines(c(0, 1), unit(c(0, 0), "native"), gp = gpar(col = "red")) |
|
156 |
-}) |
|
157 |
-``` |
|
158 |
- |
|
159 |
-For annotations which are created by `anno_points()`, `anno_barplot()` and `anno_boxplot()`, "native" unit |
|
160 |
-can be used in the decoration code. |
|
161 |
- |
|
162 |
-## Add annotation names |
|
163 |
- |
|
164 |
-By default, annotation names are not plotted along with the heatmap annotations. The reason is |
|
165 |
-if annotation names is plotted, they will located in the area of other heatmap components which |
|
166 |
-would makes the adjustment of the heatmap layout difficult. `HeatmapAnnotation()` provides a |
|
167 |
-not-so-perfect [solution](s4.heatmap_annotation.html#toc_11) for adding annotation names, however, since you can go to any component |
|
168 |
-in the heatmap list by its name, actually it is not difficult to add annotation names manually. |
|
169 |
- |
|
170 |
-Following code add annotation names on the both sides of the column annotations. The drawback is |
|
171 |
-since there is no specific component designed for annotation names, if the annotation name is too long, |
|
172 |
-it will be exceeding the figure region (but this problem can be solved by some tricks, see the |
|
173 |
-[**Examples**](s9.examples.html) vignette). |
|
174 |
- |
|
175 |
-```{r annotation_title} |
|
176 |
-df = data.frame(type1 = c(rep("a", 5), rep("b", 5)), |
|
177 |
- type2 = c(rep("A", 3), rep("B", 7))) |
|
178 |
-ha = HeatmapAnnotation(df, col = list(type1 = c("a" = "red", "b" = "blue"), |
|
179 |
- type2 = c("A" = "green", "B" = "orange"))) |
|
180 |
-Heatmap(mat, name = "ht", top_annotation = ha) |
|
181 |
-for(an in colnames(df)) { |
|
182 |
- decorate_annotation(an, { |
|
183 |
- # annotation names on the right |
|
184 |
- grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left") |
|
185 |
- # annotation names on the left |
|
186 |
- grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right") |
|
187 |
- }) |
|
188 |
-} |
|
189 |
-``` |
|
190 |
- |
|
191 |
-## Visualize distributions |
|
192 |
- |
|
193 |
-With heatmap decorations, actually you can design new graphics based on heatmaps. Following is an example: |
|
194 |
- |
|
195 |
-To visualize distribution of columns in a matrix or in a list, sometimes we use boxplot or beanplot. |
|
196 |
-Here we can also use colors to map to the density values and visualize distribution of values |
|
197 |
-in each column (or each list element) through a heatmap. Sometimes it can give you a more clear |
|
198 |
-image of your data. |
|
199 |
- |
|
200 |
-Here the package has a `densityHeatmap()` function, the usage is quite straightforward: |
|
201 |
- |
|
202 |
-```{r, density} |
|
203 |
-matrix = matrix(rnorm(100), 10); colnames(matrix) = letters[1:10] |
|
204 |
-ha = HeatmapAnnotation(df = data.frame(anno = rep(c("A", "B"), each = 5)), |
|
205 |
- col = list(anno = c("A" = "green", "B" = "orange")), |
|
206 |
- points = anno_points(runif(10))) |
|
207 |
-densityHeatmap(matrix, anno = ha) |
|
208 |
-``` |
|
209 |
- |
|
210 |
-## Session info |
|
211 |
- |
|
212 |
-```{r} |
|
213 |
-sessionInfo() |
|
214 |
-``` |
1 | 1 |
new file mode 100755 |
... | ... |
@@ -0,0 +1,216 @@ |
1 |
+<!-- |
|
2 |
+%\VignetteEngine{knitr} |
|
3 |
+%\VignetteIndexEntry{6. Heatmap Decoration} |
|
4 |
+--> |
|
5 |
+ |
|
6 |
+Heatmap Decoration |
|
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 |
+Each components of the heatmap/heatmap list has a name (unique id). You can go to any viewport |
|
33 |
+to add graphics in by specifying the heatmap/annotation name. |
|
34 |
+ |
|
35 |
+First generate a figure that almost contains all types of heatmap components. |
|
36 |
+ |
|
37 |
+```{r access_components, fig.width = 10, fig.height = 7, echo = 1:13} |
|
38 |
+library(ComplexHeatmap) |
|
39 |
+ |
|
40 |
+mat = matrix(rnorm(80, 2), 8, 10) |
|
41 |
+mat = rbind(mat, matrix(rnorm(40, -2), 4, 10)) |
|
42 |
+rownames(mat) = paste0("R", 1:12) |
|
43 |
+colnames(mat) = paste0("C", 1:10) |
|
44 |
+ |
|
45 |
+ha_column1 = HeatmapAnnotation(points = anno_points(rnorm(10))) |
|
46 |
+ht1 = Heatmap(mat, name = "ht1", km = 2, row_title = "Heatmap 1", column_title = "Heatmap 1", |
|
47 |
+ top_annotation = ha_column1) |
|
48 |
+ |
|
49 |
+ha_column2 = HeatmapAnnotation(df = data.frame(type = c(rep("a", 5), rep("b", 5))), |
|
50 |
+ col = list(type = c("a" = "red", "b" = "blue"))) |
|
51 |
+ht2 = Heatmap(mat, name = "ht2", row_title = "Heatmap 2", column_title = "Heatmap 2", |
|
52 |
+ bottom_annotation = ha_column2) |
|
53 |
+ |
|
54 |
+ht_list = ht1 + ht2 + rowAnnotation(bar = row_anno_barplot(rowMeans(mat))) |
|
55 |
+draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", |
|
56 |
+ heatmap_legend_side = "right", annotation_legend_side = "left") |
|
57 |
+ |
|
58 |
+library(GetoptLong) |
|
59 |
+seekViewport("global"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
60 |
+seekViewport("global_column_title"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
61 |
+seekViewport("global_row_title"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
62 |
+seekViewport("main_heatmap_list"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
63 |
+for(heatmap_name in c("ht1", "ht2")) { |
|
64 |
+ seekViewport(qq("heatmap_@{heatmap_name}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
65 |
+ for(i in 1:2) { |
|
66 |
+ seekViewport(qq("@{heatmap_name}_heatmap_body_@{i}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
67 |
+ seekViewport(qq("@{heatmap_name}_row_names_@{i}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ seekViewport(qq("@{heatmap_name}_column_title")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
71 |
+ seekViewport(qq("@{heatmap_name}_dend_column")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
72 |
+ seekViewport(qq("@{heatmap_name}_column_names")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
73 |
+ seekViewport(qq("legend_@{heatmap_name}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
74 |
+} |
|
75 |
+seekViewport(qq("ht1_dend_row_2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
76 |
+seekViewport(qq("ht1_dend_row_1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
77 |
+seekViewport(qq("ht1_row_title_1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
78 |
+seekViewport(qq("ht1_row_title_2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
79 |
+ |
|
80 |
+ |
|
81 |
+for(annotation_name in c("points", "type")) { |
|
82 |
+ seekViewport(qq("annotation_@{annotation_name}")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
83 |
+} |
|
84 |
+seekViewport(qq("annotation_bar_1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
85 |
+seekViewport(qq("annotation_bar_2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
86 |
+ |
|
87 |
+seekViewport(qq("legend_type")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
88 |
+seekViewport(qq("legend_ht1")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
89 |
+seekViewport(qq("legend_ht2")); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
90 |
+ |
|
91 |
+seekViewport("heatmap_legend"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
92 |
+seekViewport("annotation_legend"); grid.rect(gp = gpar(fill = NA, col = "red")) |
|
93 |
+``` |
|
94 |
+ |
|
95 |
+The components (viewports) that have names are: |
|
96 |
+ |
|
97 |
+- `global`: the viewport which contains the whole figure. |
|
98 |
+- `global_column_title`: the viewport which contains column title for the heatmap list. |
|
99 |
+- `global_row_title`: the viewport which contains row title for the heatmap list. |
|
100 |
+- `main_heatmap_list`: the viewport which contains a list of heatmaps and row annotations. |
|
101 |
+- `heatmap_@{heatmap_name}`: the viewport which contains a single heatmap |
|
102 |
+- `annotation_@{annotation_name}`: the viewport which contains an annotation on columns. |
|
103 |
+- `annotation_@{annotation_name}_@{i}`: for row annotations |
|
104 |
+- `@{heatmap_name}_heatmap_body_@{i}`: the heatmap body. |
|
105 |
+- `@{heatmap_name}_column_title`: column title for a single heatmap. |
|
106 |
+- `@{heatmap_name}_row_title_@{i}`: since a heatmap body may be splitted into several parts. `@{i}` is the index of the row slice. |
|
107 |
+- `@{heatmap_name}_dend_row_@{i}`: dendrogram for ith row slice. |
|
108 |
+- `@{heatmap_name}_dend_column`: dendrogram on columns |
|
109 |
+- `@{heatmap_name}_row_names_@{i}`: the viewport which contains row names. |
|
110 |
+- `@{heatmap_name}_column_names`: the viewport which contains column names. |
|
111 |
+- `heatmap_legend`: the viewport which contains all heatmap legends. |
|
112 |
+- `legend_@{heatmap_name}`: the viewport which contains a single heatmap legend. |
|
113 |
+- `annotation_legend`: the viewport which contains all annotation legends. |
|
114 |
+- `legend_@{annotation_name}`: the viewport which contains a single annotation legend. |
|
115 |
+ |
|
116 |
+## decorate_* functions |
|
117 |
+ |
|
118 |
+Basically, you can go to these components by `seekViewport()`, but to hide the details that is too low-level, |
|
119 |
+**ComplexHeatmap** package provides `decorate_*` family functions which makes it easy to add graphics into different components. |
|
120 |
+ |
|
121 |
+Following code add annotation names, mark one grid in the heatmap and seperate the first column clusters with two rectangles. |
|
122 |
+ |
|
123 |
+```{r components, fig.width = 10, fig.height = 7} |
|
124 |
+ht_list = draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", |
|
125 |
+ heatmap_legend_side = "right", annotation_legend_side = "left") |
|
126 |
+decorate_annotation("points", { |
|
127 |
+ grid.text("points", unit(0, "npc") - unit(2, "mm"), 0.5, |
|
128 |
+ default.units = "npc", just = "right") |
|
129 |
+}) |
|
130 |
+ |
|
131 |
+decorate_heatmap_body("ht1", { |
|
132 |
+ grid.text("outlier", 1.5/10, 2.5/4, default.units = "npc") |
|
133 |
+ grid.lines(c(0.5, 0.5), c(0, 1), gp = gpar(lty = 2, lwd = 2)) |
|
134 |
+}, slice = 2) |
|
135 |
+ |
|
136 |
+decorate_column_dend("ht1", { |
|
137 |
+ tree = column_dend(ht_list)$ht1 |
|
138 |
+ ind = cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)] |
|
139 |
+ |
|
140 |
+ first_index = function(l) which(l)[1] |
|
141 |
+ last_index = function(l) { x = which(l); x[length(x)] } |
|
142 |
+ x1 = c(first_index(ind == 1), first_index(ind == 2)) - 1 |
|
143 |
+ x2 = c(last_index(ind == 1), last_index(ind == 2)) |
|
144 |
+ grid.rect(x = x1/length(ind), width = (x2 - x1)/length(ind), just = "left", |
|
145 |
+ default.units = "npc", gp = gpar(fill = c("#FF000040", "#00FF0040"), col = NA)) |
|
146 |
+}) |
|
147 |
+ |
|
148 |
+decorate_row_names("ht2", { |
|
149 |
+ grid.rect(gp = gpar(fill = "#FF000040")) |
|
150 |
+}, slice = 2) |
|
151 |
+ |
|
152 |
+decorate_row_title("ht1", { |
|
153 |
+ grid.rect(gp = gpar(fill = "#00FF0040")) |
|
154 |
+}, slice = 1) |
|
155 |
+ |
|
156 |
+decorate_annotation("points", { |
|
157 |
+ grid.lines(c(0, 1), unit(c(0, 0), "native"), gp = gpar(col = "red")) |
|
158 |
+}) |
|
159 |
+``` |
|
160 |
+ |
|
161 |
+For annotations which are created by `anno_points()`, `anno_barplot()` and `anno_boxplot()`, "native" unit |
|
162 |
+can be used in the decoration code. |
|
163 |
+ |
|
164 |
+## Add annotation names |
|
165 |
+ |
|
166 |
+By default, annotation names are not plotted along with the heatmap annotations. The reason is |
|
167 |
+if annotation names is plotted, they will located in the area of other heatmap components which |
|
168 |
+would makes the adjustment of the heatmap layout difficult. `HeatmapAnnotation()` provides a |
|
169 |
+not-so-perfect [solution](s4.heatmap_annotation.html#toc_11) for adding annotation names, however, since you can go to any component |
|
170 |
+in the heatmap list by its name, actually it is not difficult to add annotation names manually. |
|
171 |
+ |
|
172 |
+Following code add annotation names on the both sides of the column annotations. The drawback is |
|
173 |
+since there is no specific component designed for annotation names, if the annotation name is too long, |
|
174 |
+it will be exceeding the figure region (but this problem can be solved by some tricks, see the |
|
175 |
+[**Examples**](s9.examples.html) vignette). |
|
176 |
+ |
|
177 |
+```{r annotation_title} |
|
178 |
+df = data.frame(type1 = c(rep("a", 5), rep("b", 5)), |
|
179 |
+ type2 = c(rep("A", 3), rep("B", 7))) |
|
180 |
+ha = HeatmapAnnotation(df, col = list(type1 = c("a" = "red", "b" = "blue"), |
|
181 |
+ type2 = c("A" = "green", "B" = "orange"))) |
|
182 |
+Heatmap(mat, name = "ht", top_annotation = ha) |
|
183 |
+for(an in colnames(df)) { |
|
184 |
+ decorate_annotation(an, { |
|
185 |
+ # annotation names on the right |
|
186 |
+ grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left") |
|
187 |
+ # annotation names on the left |
|
188 |
+ grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right") |
|
189 |
+ }) |
|
190 |
+} |
|
191 |
+``` |
|
192 |
+ |
|
193 |
+## Visualize distributions |
|
194 |
+ |
|
195 |
+With heatmap decorations, actually you can design new graphics based on heatmaps. Following is an example: |
|
196 |
+ |
|
197 |
+To visualize distribution of columns in a matrix or in a list, sometimes we use boxplot or beanplot. |
|
198 |
+Here we can also use colors to map to the density values and visualize distribution of values |
|
199 |
+in each column (or each list element) through a heatmap. Sometimes it can give you a more clear |
|
200 |
+image of your data. |
|
201 |
+ |
|
202 |
+Here the package has a `densityHeatmap()` function, the usage is quite straightforward: |
|
203 |
+ |
|
204 |
+```{r, density} |
|
205 |
+matrix = matrix(rnorm(100), 10); colnames(matrix) = letters[1:10] |
|
206 |
+ha = HeatmapAnnotation(df = data.frame(anno = rep(c("A", "B"), each = 5)), |
|
207 |
+ col = list(anno = c("A" = "green", "B" = "orange")), |
|
208 |
+ points = anno_points(runif(10))) |
|
209 |
+densityHeatmap(matrix, anno = ha) |
|
210 |
+``` |
|
211 |
+ |
|
212 |
+## Session info |
|
213 |
+ |
|
214 |
+```{r} |
|
215 |
+sessionInfo() |
|
216 |
+``` |