Browse code

a backup push

Zuguang Gu authored on 18/09/2018 10:40:29
Showing 1 changed files
1 1
deleted file mode 100755
... ...
@@ -1,225 +0,0 @@
1
-<!--
2
-%\VignetteEngine{knitr}
3
-%\VignetteIndexEntry{9. More Examples}
4
-
5
-More Examples of Making Complex Heatmaps
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
-
16
-library(knitr)
17
-knitr::opts_chunk$set(
18
-    error = FALSE,
19
-    tidy  = FALSE,
20
-    message = FALSE,
21
-    fig.align = "center",
22
-    fig.width = 5,
23
-    fig.height = 5)
24
-options(markdown.HTML.stylesheet = "custom.css")
25
-
26
-options(width = 100)
27
-```
28
-
29
-In the supplementaries of [the ComplexHeatmap paper](http://bioinformatics.oxfordjournals.org/content/early/2016/05/20/bioinformatics.btw313.abstract), there are four comprehensive examples which are applied
30
-on real-world high-throughput datasets. [The examples can be found here.](http://jokergoo.github.io/supplementary/ComplexHeatmap-supplementary1-4/index.html)
31
-
32
-Also [my blog](http://jokergoo.github.io/blog.html) has some examples and tips for making better complex heatmaps.
33
-
34
-
35
-### Add more information for gene expression matrix
36
-
37
-Heatmaps are very popular to visualize gene expression matrix. 
38
-Rows in the matrix correspond to genes and more information on these genes can be attached after the expression
39
-heatmap.
40
-
41
-In following example, the big heatmap visualize relative expression for genes, then the next is the absolute expression.
42
-Also gene length and gene type (i.e. protein coding or lincRNA) are visualized.
43
-
44
-
45
-```{r expression_example, fig.width = 10, fig.height = 8}
46
-library(ComplexHeatmap)
47
-library(circlize)
48
-
49
-expr = readRDS(paste0(system.file(package = "ComplexHeatmap"), "/extdata/gene_expression.rds"))
50
-mat = as.matrix(expr[, grep("cell", colnames(expr))])
51
-base_mean = rowMeans(mat)
52
-mat_scaled = t(apply(mat, 1, scale))
53
-
54
-type = gsub("s\\d+_", "", colnames(mat))
55
-ha = HeatmapAnnotation(df = data.frame(type = type))
56
-
57
-Heatmap(mat_scaled, name = "expression", km = 5, col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")),
58
-    top_annotation = ha, top_annotation_height = unit(4, "mm"), 
59
-    show_row_names = FALSE, show_column_names = FALSE) +
60
-Heatmap(base_mean, name = "base_mean", show_row_names = FALSE, width = unit(5, "mm")) +
61
-Heatmap(expr$length, name = "length", col = colorRamp2(c(0, 1000000), c("white", "orange")),
62
-    heatmap_legend_param = list(at = c(0, 200000, 400000, 60000, 800000, 1000000), 
63
-                                labels = c("0kb", "200kb", "400kb", "600kb", "800kb", "1mb")),
64
-    width = unit(5, "mm")) +
65
-Heatmap(expr$type, name = "type", width = unit(5, "mm"))
66
-```
67
-
68
-### Visualize genomic regions and other correspondance
69
-
70
-Following example visualizes correlation between methylation and expression, as well as other annotation information (data are randomly generated). In the heatmap, each row corresponds to a differentially methylated regions (DMRs). 
71
-From left to right, heatmaps are:
72
-
73
-1. methylation for each DMR (by rows) in samples.
74
-2. direction of the methylation (one column heatmap), i.e. is methylation hyper in tumor or hypo?
75
-3. expression for the genes that are associated with corresponding DMRs (e.g. closest gene).
76
-4. significance for the correlation between methylation and expression (-log10(p-value)).
77
-5. type of genes, i.e. is the gene a protein coding gene or a lincRNA?
78
-6. annotation to gene models, i.e. is the DMR located in the intragenic region of the corresponding gene or the DMR is intergenic?
79
-7. distance from the DMR to the TSS of the corresponding gene.
80
-8. overlapping between DMRs and enhancers (Color shows how much the DMR is covered by the enhancers).
81
-
82
-
83
-```{r, fig.width = 10, fig.height = 8, echo = FALSE, results = "hide"}
84
-library(circlize)
85
-library(RColorBrewer)
86
-
87
-lt = readRDS(paste0(system.file(package = "ComplexHeatmap"), "/extdata/meth.rds"))
88
-list2env(lt, envir = environment())
89
-
90
-ha = HeatmapAnnotation(df = data.frame(type = c(rep("Tumor", 10), rep("Control", 10))), 
91
-    col = list(type = c("Tumor" = "red", "Control" = "blue")))
92
-ha2 = HeatmapAnnotation(df = data.frame(type = c(rep("Tumor", 10), rep("Control", 10))), 
93
-    col = list(type = c("Tumor" = "red", "Control" = "blue")), show_legend = FALSE)
94
-
95
-# column order of the methylation matrix which will be assigned to the expressio matrix
96
-column_tree = hclust(dist(t(meth)))
97
-
98
-ht_list = 
99
-    Heatmap(meth, name = "methylation", col = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red")),
100
-        cluster_columns = column_tree, top_annotation = ha, column_names_gp = gpar(fontsize = 8), km = 5, 
101
-        column_title = "Methylation", column_title_gp = gpar(fontsize = 10), 
102
-        row_title_gp = gpar(fontsize = 10)) +
103
-    Heatmap(direction, name = "direction", col = c("hyper" = "red", "hypo" = "blue"), 
104
-        column_names_gp = gpar(fontsize = 8)) +
105
-    Heatmap(expr[, column_tree$order], name = "expression", col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")),
106
-        cluster_columns = FALSE, top_annotation = ha2, column_names_gp = gpar(fontsize = 8), 
107
-        column_title = "Expression", column_title_gp = gpar(fontsize = 10)) +
108
-    Heatmap(cor_pvalue, name = "-log10(cor_p)", col = colorRamp2(c(0, 2, 4), c("white", "white", "red")), 
109
-        column_names_gp = gpar(fontsize = 8)) +
110
-    Heatmap(gene_type, name = "gene type", col = brewer.pal(length(unique(gene_type)), "Set1"), 
111
-        column_names_gp = gpar(fontsize = 8)) +
112
-    Heatmap(anno, name = "anno_gene", col = brewer.pal(length(unique(anno)), "Set2"), 
113
-        column_names_gp = gpar(fontsize = 8)) +
114
-    Heatmap(dist, name = "dist_tss", col = colorRamp2(c(0, 10000), c("black", "white")), 
115
-        column_names_gp = gpar(fontsize = 8)) +
116
-    Heatmap(enhancer, name = "anno_enhancer", col = colorRamp2(c(0, 1), c("white", "orange")), 
117
-        cluster_columns = FALSE, column_names_gp = gpar(fontsize = 8), column_title = "Enhancer", 
118
-        column_title_gp = gpar(fontsize = 10))
119
-
120
-ht_global_opt(heatmap_legend_title_gp = gpar(fontsize = 8, fontface = "bold"), 
121
-              heatmap_legend_labels_gp = gpar(fontsize = 8))
122
-draw(ht_list, newpage = FALSE, column_title = "Correspondence between methylation, expression and other genomic features", 
123
-    column_title_gp = gpar(fontsize = 12, fontface = "bold"), heatmap_legend_side = "bottom")
124
-invisible(ht_global_opt(RESET = TRUE))
125
-```
126
-
127
-
128
-## Combine pvclust and heatmap
129
-
130
-**pvclust** package provides a robust way to test the stability of the clustering
131
-by random sampling from original data. Here you can organize the heatmap by the clustering
132
-returned from `pvclust()`.
133
-
134
-```{r}
135
-library(ComplexHeatmap)
136
-
137
-library(MASS)
138
-library(pvclust)
139
-data(Boston)
140
-boston.pv <- pvclust(Boston, nboot=100)
141
-plot(boston.pv)
142
-```
143
-
144
-Since by default `pvclust` clusters columns by 'correlation' method, we scale columns for
145
-`Boston` data set to see the relative trend.
146
-
147
-```{r}
148
-Boston_scaled = apply(Boston, 2, scale)
149
-Heatmap(Boston_scaled, cluster_columns = boston.pv$hclust, heatmap_legend_param = list(title = "Boston"))
150
-```
151
-
152
-## Make a same plot as heatmap()
153
-
154
-```{r}
155
-set.seed(123)
156
-mat = matrix(rnorm(100), 10)
157
-heatmap(mat, col = topo.colors(50))
158
-```
159
-
160
-Compare to the native `heatmap()`, `Heatmap()` can give more accurate interpolation
161
-for colors for continous values.
162
-
163
-```{r}
164
-Heatmap(mat, col = topo.colors(50), color_space = "sRGB",
165
-    row_dend_width = unit(2, "cm"), 
166
-    column_dend_height = unit(2, "cm"), row_dend_reorder = TRUE,
167
-    column_dend_reorder = TRUE)
168
-```
169
-
170
-## The measles vaccine heatmap
171
-
172
-Following code reproduces the heatmap introduced [here](https://biomickwatson.wordpress.com/2015/04/09/recreating-a-famous-visualisation/) and [here](https://benjaminlmoore.wordpress.com/2015/04/09/recreating-the-vaccination-heatmaps-in-r/).
173
-
174
-```{r, fig.width = 10, fig.height = 8}
175
-mat = readRDS(paste0(system.file("extdata", package = "ComplexHeatmap"), "/measles.rds"))
176
-ha1 = HeatmapAnnotation(dist1 = anno_barplot(colSums(mat), bar_width = 1, gp = gpar(col = NA, fill = "#FFE200"), 
177
-    border = FALSE, axis = TRUE))
178
-ha2 = rowAnnotation(dist2 = anno_barplot(rowSums(mat), bar_width = 1, gp = gpar(col = NA, fill = "#FFE200"), 
179
-    border = FALSE, which = "row", axis = TRUE), width = unit(1, "cm"))
180
-ha_column = HeatmapAnnotation(cn = function(index) {
181
-    year = as.numeric(colnames(mat))
182
-    which_decade = which(year %% 10 == 0)
183
-    grid.text(year[which_decade], which_decade/length(year), 1, just = c("center", "top"))
184
-})
185
-Heatmap(mat, name = "cases", col = colorRamp2(c(0, 800, 1000, 127000), c("white", "cornflowerblue", "yellow", "red")),
186
-    cluster_columns = FALSE, show_row_dend = FALSE, rect_gp = gpar(col= "white"), show_column_names = FALSE,
187
-    row_names_side = "left", row_names_gp = gpar(fontsize = 10),
188
-    column_title = 'Measles cases in US states 1930-2001\nVaccine introduced 1961',
189
-    top_annotation = ha1, top_annotation_height = unit(1, "cm"),
190
-    bottom_annotation = ha_column, bottom_annotation_height = grobHeight(textGrob("1900"))) + ha2
191
-
192
-decorate_heatmap_body("cases", {
193
-    i = which(colnames(mat) == "1961")
194
-    x = i/ncol(mat)
195
-    grid.lines(c(x, x), c(0, 1), gp = gpar(lwd = 2))
196
-    grid.text("Vaccine introduced", x, unit(1, "npc") + unit(5, "mm"))
197
-})
198
-```
199
-
200
-## What if my annotation name is too long?
201
-
202
-There is no space allocated for annotation name, but when the annotation name is too long,
203
-you can add paddings of the whole plot to give empty spaces for the annotation names.
204
-
205
-```{r, fig.width = 7}
206
-ha = HeatmapAnnotation(df = data.frame(a_long_long_long_annotation_name = runif(10)),
207
-    show_legend = FALSE)
208
-ht = Heatmap(matrix(rnorm(100), 10), name = "foo", top_annotation = ha)
209
-# because the default width for row cluster is 1cm
210
-padding = unit.c(unit(2, "mm"), grobWidth(textGrob("a_long_long_long_annotation_name")) - unit(1, "cm"),
211
-    unit(c(2, 2), "mm"))
212
-draw(ht, padding = padding)
213
-decorate_annotation("a_long_long_long_annotation_name", {
214
-    grid.text("a_long_long_long_annotation_name", 0, 0.5, just = "right")
215
-})
216
-```
217
-
218
-
219
-## Session info
220
-
221
-```{r}
222
-sessionInfo()
223
-```
Browse code

HeatmapAnnotation() can directly add annotation names

Zuguang Gu authored on 14/06/2016 14:00:31
Showing 1 changed files
... ...
@@ -28,6 +28,11 @@ options(markdown.HTML.stylesheet = "custom.css")
28 28
 options(width = 100)
29 29
 ```
30 30
 
31
+In the supplementaries of [the ComplexHeatmap paper](http://bioinformatics.oxfordjournals.org/content/early/2016/05/20/bioinformatics.btw313.abstract), there are four comprehensive examples which are applied
32
+on real-world high-throughput datasets. [The examples can be found here.](http://jokergoo.github.io/supplementary/ComplexHeatmap-supplementary1-4/index.html)
33
+
34
+Also [my blog](http://jokergoo.github.io/blog.html) has some examples and tips for making better complex heatmaps.
35
+
31 36
 
32 37
 ### Add more information for gene expression matrix
33 38
 
Browse code

argument are explicitly specified instead of using '...'

Zuguang Gu authored on 14/04/2016 09:08:33
Showing 1 changed files
... ...
@@ -77,7 +77,7 @@ From left to right, heatmaps are:
77 77
 8. overlapping between DMRs and enhancers (Color shows how much the DMR is covered by the enhancers).
78 78
 
79 79
 
80
-```{r, fig.width = 10, fig.height = 8, echo = FALSE}
80
+```{r, fig.width = 10, fig.height = 8, echo = FALSE, results = "hide"}
81 81
 library(circlize)
82 82
 library(RColorBrewer)
83 83
 
Browse code

give warnings if there are duplicated heatmap names

Zuguang Gu authored on 27/03/2016 15:02:46
Showing 1 changed files
... ...
@@ -118,7 +118,7 @@ ht_global_opt(heatmap_legend_title_gp = gpar(fontsize = 8, fontface = "bold"),
118 118
               heatmap_legend_labels_gp = gpar(fontsize = 8))
119 119
 draw(ht_list, newpage = FALSE, column_title = "Correspondence between methylation, expression and other genomic features", 
120 120
     column_title_gp = gpar(fontsize = 12, fontface = "bold"), heatmap_legend_side = "bottom")
121
-ht_global_opt(RESET = TRUE)
121
+invisible(ht_global_opt(RESET = TRUE))
122 122
 ```
123 123
 
124 124
 
Browse code

Merge branch 'master' into devel

git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ComplexHeatmap@108253 bc3139a8-67e5-0310-9ffc-ced21a209358

z.gu authored on 07/09/2015 20:39:32
Showing 1 changed files
1 1
old mode 100644
2 2
new mode 100755
Browse code

Merge branch 'master' into devel

git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ComplexHeatmap@108252 bc3139a8-67e5-0310-9ffc-ced21a209358

z.gu authored on 07/09/2015 20:38:03
Showing 1 changed files
... ...
@@ -1,9 +1,9 @@
1 1
 <!--
2 2
 %\VignetteEngine{knitr}
3
-%\VignetteIndexEntry{9. Examples of Making Complex Heatmaps}
3
+%\VignetteIndexEntry{9. More Examples}
4 4
 -->
5 5
 
6
-Examples of Making Complex Heatmaps
6
+More Examples of Making Complex Heatmaps
7 7
 ========================================
8 8
 
9 9
 **Author**: Zuguang Gu ( z.gu@dkfz.de )
... ...
@@ -31,8 +31,13 @@ options(width = 100)
31 31
 
32 32
 ### Add more information for gene expression matrix
33 33
 
34
-In gene expression matrix, rows correspond to genes. More information about genes can be attached after the expression
35
-heatmap such as gene length and type of genes.
34
+Heatmaps are very popular to visualize gene expression matrix. 
35
+Rows in the matrix correspond to genes and more information on these genes can be attached after the expression
36
+heatmap.
37
+
38
+In following example, the big heatmap visualize relative expression for genes, then the next is the absolute expression.
39
+Also gene length and gene type (i.e. protein coding or lincRNA) are visualized.
40
+
36 41
 
37 42
 ```{r expression_example, fig.width = 10, fig.height = 8}
38 43
 library(ComplexHeatmap)
... ...
@@ -40,23 +45,26 @@ library(circlize)
40 45
 
41 46
 expr = readRDS(paste0(system.file(package = "ComplexHeatmap"), "/extdata/gene_expression.rds"))
42 47
 mat = as.matrix(expr[, grep("cell", colnames(expr))])
48
+base_mean = rowMeans(mat)
49
+mat_scaled = t(apply(mat, 1, scale))
43 50
 
44 51
 type = gsub("s\\d+_", "", colnames(mat))
45 52
 ha = HeatmapAnnotation(df = data.frame(type = type))
46 53
 
47
-Heatmap(mat, name = "expression", km = 5, top_annotation = ha, 
48
-    top_annotation_height = unit(4, "mm"), show_row_names = FALSE, 
49
-    show_column_names = FALSE) +
50
-Heatmap(expr$length, name = "length", col = colorRamp2(c(0, 100000), c("white", "orange")),
54
+Heatmap(mat_scaled, name = "expression", km = 5, col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")),
55
+    top_annotation = ha, top_annotation_height = unit(4, "mm"), 
56
+    show_row_names = FALSE, show_column_names = FALSE) +
57
+Heatmap(base_mean, name = "base_mean", show_row_names = FALSE, width = unit(5, "mm")) +
58
+Heatmap(expr$length, name = "length", col = colorRamp2(c(0, 1000000), c("white", "orange")),
59
+    heatmap_legend_param = list(at = c(0, 200000, 400000, 60000, 800000, 1000000), 
60
+                                labels = c("0kb", "200kb", "400kb", "600kb", "800kb", "1mb")),
51 61
     width = unit(5, "mm")) +
52
-Heatmap(expr$type, name = "type", width = unit(5, "mm")) +
53
-Heatmap(expr$chr, name = "chr", col = rand_color(length(unique(expr$chr))), 
54
-    width = unit(5, "mm"))
62
+Heatmap(expr$type, name = "type", width = unit(5, "mm"))
55 63
 ```
56 64
 
57 65
 ### Visualize genomic regions and other correspondance
58 66
 
59
-Following examples visualizes correlation between methylation and expression, as well as other annotation information (data are randomly generated). In the heatmap, each row corresponds to a differentially methylated regions (DMRs). 
67
+Following example visualizes correlation between methylation and expression, as well as other annotation information (data are randomly generated). In the heatmap, each row corresponds to a differentially methylated regions (DMRs). 
60 68
 From left to right, heatmaps are:
61 69
 
62 70
 1. methylation for each DMR (by rows) in samples.
... ...
@@ -69,7 +77,7 @@ From left to right, heatmaps are:
69 77
 8. overlapping between DMRs and enhancers (Color shows how much the DMR is covered by the enhancers).
70 78
 
71 79
 
72
-```{r, fig.width = 10, fig.height = 8}
80
+```{r, fig.width = 10, fig.height = 8, echo = FALSE}
73 81
 library(circlize)
74 82
 library(RColorBrewer)
75 83
 
... ...
@@ -81,18 +89,19 @@ ha = HeatmapAnnotation(df = data.frame(type = c(rep("Tumor", 10), rep("Control",
81 89
 ha2 = HeatmapAnnotation(df = data.frame(type = c(rep("Tumor", 10), rep("Control", 10))), 
82 90
     col = list(type = c("Tumor" = "red", "Control" = "blue")), show_legend = FALSE)
83 91
 
84
-# to make column order of expression matrix is the same as methylation matrix
92
+# column order of the methylation matrix which will be assigned to the expressio matrix
85 93
 column_tree = hclust(dist(t(meth)))
86 94
 
87 95
 ht_list = 
88 96
     Heatmap(meth, name = "methylation", col = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red")),
89 97
         cluster_columns = column_tree, top_annotation = ha, column_names_gp = gpar(fontsize = 8), km = 5, 
90
-        column_title = "Methylation", column_title_gp = gpar(fontsize = 10), row_title_gp = gpar(fontsize = 10)) +
98
+        column_title = "Methylation", column_title_gp = gpar(fontsize = 10), 
99
+        row_title_gp = gpar(fontsize = 10)) +
91 100
     Heatmap(direction, name = "direction", col = c("hyper" = "red", "hypo" = "blue"), 
92 101
         column_names_gp = gpar(fontsize = 8)) +
93 102
     Heatmap(expr[, column_tree$order], name = "expression", col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")),
94
-        cluster_columns = FALSE, top_annotation = ha2, column_names_gp = gpar(fontsize = 8), column_title = "Expression",
95
-        column_title_gp = gpar(fontsize = 10)) +
103
+        cluster_columns = FALSE, top_annotation = ha2, column_names_gp = gpar(fontsize = 8), 
104
+        column_title = "Expression", column_title_gp = gpar(fontsize = 10)) +
96 105
     Heatmap(cor_pvalue, name = "-log10(cor_p)", col = colorRamp2(c(0, 2, 4), c("white", "white", "red")), 
97 106
         column_names_gp = gpar(fontsize = 8)) +
98 107
     Heatmap(gene_type, name = "gene type", col = brewer.pal(length(unique(gene_type)), "Set1"), 
... ...
@@ -149,7 +158,8 @@ Compare to the native `heatmap()`, `Heatmap()` can give more accurate interpolat
149 158
 for colors for continous values.
150 159
 
151 160
 ```{r}
152
-Heatmap(mat, col = topo.colors(50), row_dend_width = unit(2, "cm"), 
161
+Heatmap(mat, col = topo.colors(50), color_space = "sRGB",
162
+    row_dend_width = unit(2, "cm"), 
153 163
     column_dend_height = unit(2, "cm"), row_dend_reorder = TRUE,
154 164
     column_dend_reorder = TRUE)
155 165
 ```
... ...
@@ -189,7 +199,7 @@ decorate_heatmap_body("cases", {
189 199
 There is no space allocated for annotation name, but when the annotation name is too long,
190 200
 you can add paddings of the whole plot to give empty spaces for the annotation names.
191 201
 
192
-```{r, fig.width = 10}
202
+```{r, fig.width = 7}
193 203
 ha = HeatmapAnnotation(df = data.frame(a_long_long_long_annotation_name = runif(10)),
194 204
     show_legend = FALSE)
195 205
 ht = Heatmap(matrix(rnorm(100), 10), name = "foo", top_annotation = ha)
... ...
@@ -201,3 +211,10 @@ decorate_annotation("a_long_long_long_annotation_name", {
201 211
     grid.text("a_long_long_long_annotation_name", 0, 0.5, just = "right")
202 212
 })
203 213
 ```
214
+
215
+
216
+## Session info
217
+
218
+```{r}
219
+sessionInfo()
220
+```
Browse code

resolve conflict from master branch

git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ComplexHeatmap@108131 bc3139a8-67e5-0310-9ffc-ced21a209358

z.gu authored on 03/09/2015 20:32:44
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,203 @@
1
+<!--
2
+%\VignetteEngine{knitr}
3
+%\VignetteIndexEntry{9. Examples of Making Complex Heatmaps}
4
+-->
5
+
6
+Examples of Making Complex Heatmaps
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
+
18
+library(knitr)
19
+knitr::opts_chunk$set(
20
+    error = FALSE,
21
+    tidy  = FALSE,
22
+    message = FALSE,
23
+    fig.align = "center",
24
+    fig.width = 5,
25
+    fig.height = 5)
26
+options(markdown.HTML.stylesheet = "custom.css")
27
+
28
+options(width = 100)
29
+```
30
+
31
+
32
+### Add more information for gene expression matrix
33
+
34
+In gene expression matrix, rows correspond to genes. More information about genes can be attached after the expression
35
+heatmap such as gene length and type of genes.
36
+
37
+```{r expression_example, fig.width = 10, fig.height = 8}
38
+library(ComplexHeatmap)
39
+library(circlize)
40
+
41
+expr = readRDS(paste0(system.file(package = "ComplexHeatmap"), "/extdata/gene_expression.rds"))
42
+mat = as.matrix(expr[, grep("cell", colnames(expr))])
43
+
44
+type = gsub("s\\d+_", "", colnames(mat))
45
+ha = HeatmapAnnotation(df = data.frame(type = type))
46
+
47
+Heatmap(mat, name = "expression", km = 5, top_annotation = ha, 
48
+    top_annotation_height = unit(4, "mm"), show_row_names = FALSE, 
49
+    show_column_names = FALSE) +
50
+Heatmap(expr$length, name = "length", col = colorRamp2(c(0, 100000), c("white", "orange")),
51
+    width = unit(5, "mm")) +
52
+Heatmap(expr$type, name = "type", width = unit(5, "mm")) +
53
+Heatmap(expr$chr, name = "chr", col = rand_color(length(unique(expr$chr))), 
54
+    width = unit(5, "mm"))
55
+```
56
+
57
+### Visualize genomic regions and other correspondance
58
+
59
+Following examples visualizes correlation between methylation and expression, as well as other annotation information (data are randomly generated). In the heatmap, each row corresponds to a differentially methylated regions (DMRs). 
60
+From left to right, heatmaps are:
61
+
62
+1. methylation for each DMR (by rows) in samples.
63
+2. direction of the methylation (one column heatmap), i.e. is methylation hyper in tumor or hypo?
64
+3. expression for the genes that are associated with corresponding DMRs (e.g. closest gene).
65
+4. significance for the correlation between methylation and expression (-log10(p-value)).
66
+5. type of genes, i.e. is the gene a protein coding gene or a lincRNA?
67
+6. annotation to gene models, i.e. is the DMR located in the intragenic region of the corresponding gene or the DMR is intergenic?
68
+7. distance from the DMR to the TSS of the corresponding gene.
69
+8. overlapping between DMRs and enhancers (Color shows how much the DMR is covered by the enhancers).
70
+
71
+
72
+```{r, fig.width = 10, fig.height = 8}
73
+library(circlize)
74
+library(RColorBrewer)
75
+
76
+lt = readRDS(paste0(system.file(package = "ComplexHeatmap"), "/extdata/meth.rds"))
77
+list2env(lt, envir = environment())
78
+
79
+ha = HeatmapAnnotation(df = data.frame(type = c(rep("Tumor", 10), rep("Control", 10))), 
80
+    col = list(type = c("Tumor" = "red", "Control" = "blue")))
81
+ha2 = HeatmapAnnotation(df = data.frame(type = c(rep("Tumor", 10), rep("Control", 10))), 
82
+    col = list(type = c("Tumor" = "red", "Control" = "blue")), show_legend = FALSE)
83
+
84
+# to make column order of expression matrix is the same as methylation matrix
85
+column_tree = hclust(dist(t(meth)))
86
+
87
+ht_list = 
88
+    Heatmap(meth, name = "methylation", col = colorRamp2(c(0, 0.5, 1), c("blue", "white", "red")),
89
+        cluster_columns = column_tree, top_annotation = ha, column_names_gp = gpar(fontsize = 8), km = 5, 
90
+        column_title = "Methylation", column_title_gp = gpar(fontsize = 10), row_title_gp = gpar(fontsize = 10)) +
91
+    Heatmap(direction, name = "direction", col = c("hyper" = "red", "hypo" = "blue"), 
92
+        column_names_gp = gpar(fontsize = 8)) +
93
+    Heatmap(expr[, column_tree$order], name = "expression", col = colorRamp2(c(-2, 0, 2), c("green", "white", "red")),
94
+        cluster_columns = FALSE, top_annotation = ha2, column_names_gp = gpar(fontsize = 8), column_title = "Expression",
95
+        column_title_gp = gpar(fontsize = 10)) +
96
+    Heatmap(cor_pvalue, name = "-log10(cor_p)", col = colorRamp2(c(0, 2, 4), c("white", "white", "red")), 
97
+        column_names_gp = gpar(fontsize = 8)) +
98
+    Heatmap(gene_type, name = "gene type", col = brewer.pal(length(unique(gene_type)), "Set1"), 
99
+        column_names_gp = gpar(fontsize = 8)) +
100
+    Heatmap(anno, name = "anno_gene", col = brewer.pal(length(unique(anno)), "Set2"), 
101
+        column_names_gp = gpar(fontsize = 8)) +
102
+    Heatmap(dist, name = "dist_tss", col = colorRamp2(c(0, 10000), c("black", "white")), 
103
+        column_names_gp = gpar(fontsize = 8)) +
104
+    Heatmap(enhancer, name = "anno_enhancer", col = colorRamp2(c(0, 1), c("white", "orange")), 
105
+        cluster_columns = FALSE, column_names_gp = gpar(fontsize = 8), column_title = "Enhancer", 
106
+        column_title_gp = gpar(fontsize = 10))
107
+
108
+ht_global_opt(heatmap_legend_title_gp = gpar(fontsize = 8, fontface = "bold"), 
109
+              heatmap_legend_labels_gp = gpar(fontsize = 8))
110
+draw(ht_list, newpage = FALSE, column_title = "Correspondence between methylation, expression and other genomic features", 
111
+    column_title_gp = gpar(fontsize = 12, fontface = "bold"), heatmap_legend_side = "bottom")
112
+ht_global_opt(RESET = TRUE)
113
+```
114
+
115
+
116
+## Combine pvclust and heatmap
117
+
118
+**pvclust** package provides a robust way to test the stability of the clustering
119
+by random sampling from original data. Here you can organize the heatmap by the clustering
120
+returned from `pvclust()`.
121
+
122
+```{r}
123
+library(ComplexHeatmap)
124
+
125
+library(MASS)
126
+library(pvclust)
127
+data(Boston)
128
+boston.pv <- pvclust(Boston, nboot=100)
129
+plot(boston.pv)
130
+```
131
+
132
+Since by default `pvclust` clusters columns by 'correlation' method, we scale columns for
133
+`Boston` data set to see the relative trend.
134
+
135
+```{r}
136
+Boston_scaled = apply(Boston, 2, scale)
137
+Heatmap(Boston_scaled, cluster_columns = boston.pv$hclust, heatmap_legend_param = list(title = "Boston"))
138
+```
139
+
140
+## Make a same plot as heatmap()
141
+
142
+```{r}
143
+set.seed(123)
144
+mat = matrix(rnorm(100), 10)
145
+heatmap(mat, col = topo.colors(50))
146
+```
147
+
148
+Compare to the native `heatmap()`, `Heatmap()` can give more accurate interpolation
149
+for colors for continous values.
150
+
151
+```{r}
152
+Heatmap(mat, col = topo.colors(50), row_dend_width = unit(2, "cm"), 
153
+    column_dend_height = unit(2, "cm"), row_dend_reorder = TRUE,
154
+    column_dend_reorder = TRUE)
155
+```
156
+
157
+## The measles vaccine heatmap
158
+
159
+Following code reproduces the heatmap introduced [here](https://biomickwatson.wordpress.com/2015/04/09/recreating-a-famous-visualisation/) and [here](https://benjaminlmoore.wordpress.com/2015/04/09/recreating-the-vaccination-heatmaps-in-r/).
160
+
161
+```{r, fig.width = 10, fig.height = 8}
162
+mat = readRDS(paste0(system.file("extdata", package = "ComplexHeatmap"), "/measles.rds"))
163
+ha1 = HeatmapAnnotation(dist1 = anno_barplot(colSums(mat), bar_width = 1, gp = gpar(col = NA, fill = "#FFE200"), 
164
+    border = FALSE, axis = TRUE))
165
+ha2 = rowAnnotation(dist2 = anno_barplot(rowSums(mat), bar_width = 1, gp = gpar(col = NA, fill = "#FFE200"), 
166
+    border = FALSE, which = "row", axis = TRUE), width = unit(1, "cm"))
167
+ha_column = HeatmapAnnotation(cn = function(index) {
168
+    year = as.numeric(colnames(mat))
169
+    which_decade = which(year %% 10 == 0)
170
+    grid.text(year[which_decade], which_decade/length(year), 1, just = c("center", "top"))
171
+})
172
+Heatmap(mat, name = "cases", col = colorRamp2(c(0, 800, 1000, 127000), c("white", "cornflowerblue", "yellow", "red")),
173
+    cluster_columns = FALSE, show_row_dend = FALSE, rect_gp = gpar(col= "white"), show_column_names = FALSE,
174
+    row_names_side = "left", row_names_gp = gpar(fontsize = 10),
175
+    column_title = 'Measles cases in US states 1930-2001\nVaccine introduced 1961',
176
+    top_annotation = ha1, top_annotation_height = unit(1, "cm"),
177
+    bottom_annotation = ha_column, bottom_annotation_height = grobHeight(textGrob("1900"))) + ha2
178
+
179
+decorate_heatmap_body("cases", {
180
+    i = which(colnames(mat) == "1961")
181
+    x = i/ncol(mat)
182
+    grid.lines(c(x, x), c(0, 1), gp = gpar(lwd = 2))
183
+    grid.text("Vaccine introduced", x, unit(1, "npc") + unit(5, "mm"))
184
+})
185
+```
186
+
187
+## What if my annotation name is too long?
188
+
189
+There is no space allocated for annotation name, but when the annotation name is too long,
190
+you can add paddings of the whole plot to give empty spaces for the annotation names.
191
+
192
+```{r, fig.width = 10}
193
+ha = HeatmapAnnotation(df = data.frame(a_long_long_long_annotation_name = runif(10)),
194
+    show_legend = FALSE)
195
+ht = Heatmap(matrix(rnorm(100), 10), name = "foo", top_annotation = ha)
196
+# because the default width for row cluster is 1cm
197
+padding = unit.c(unit(2, "mm"), grobWidth(textGrob("a_long_long_long_annotation_name")) - unit(1, "cm"),
198
+    unit(c(2, 2), "mm"))
199
+draw(ht, padding = padding)
200
+decorate_annotation("a_long_long_long_annotation_name", {
201
+    grid.text("a_long_long_long_annotation_name", 0, 0.5, just = "right")
202
+})
203
+```