Browse code

Commit made by the Bioconductor Git-SVN bridge.

Commit id: 3429d28233ef2cbeafcd05307042d7c7aa54a2a2

add decorate* family functions



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

z.gu authored on 06/08/2015 16:40:59
Showing 69 changed files

... ...
@@ -1,8 +1,8 @@
1 1
 Package: ComplexHeatmap
2 2
 Type: Package
3 3
 Title: Making Complex Heatmaps
4
-Version: 1.2.7
5
-Date: 2015-7-18
4
+Version: 1.2.8
5
+Date: 2015-8-6
6 6
 Author: Zuguang Gu
7 7
 Maintainer: Zuguang Gu <z.gu@dkfz.de>
8 8
 Depends: R (>= 3.1.2), grid, graphics, stats, grDevices
... ...
@@ -17,6 +17,6 @@ Description: Complex heatmaps are efficient to visualize associations
17 17
 biocViews: Software, Visualization, Sequencing
18 18
 URL: https://github.com/jokergoo/ComplexHeatmap
19 19
 License: GPL (>= 2)
20
-Packaged: 2015-7-18 00:00:00 UTC; Administrator
20
+Packaged: 2015-8-6 00:00:00 UTC; Administrator
21 21
 Repository: Bioconductor
22
-Date/Publication: 2015-7-18 00:00:00
22
+Date/Publication: 2015-8-6 00:00:00
... ...
@@ -19,22 +19,25 @@ exportMethods(color_mapping_legend)
19 19
 exportMethods(prepare)
20 20
 exportMethods(heatmap_legend_size)
21 21
 export(densityHeatmap)
22
+export(decorate_annotation)
22 23
 exportClasses(HeatmapList)
23 24
 export(HeatmapList)
24 25
 exportMethods(map_to_colors)
26
+export(decorate_hclust)
25 27
 exportClasses(Heatmap)
26 28
 export(Heatmap)
27
-exportMethods(component_height)
29
+export(decorate_dimnames)
28 30
 exportMethods(draw_annotation_legend)
31
+exportMethods(component_height)
32
+exportMethods(draw_hclust)
29 33
 exportClasses(SingleAnnotation)
30 34
 export(SingleAnnotation)
31
-exportMethods(draw_hclust)
32 35
 exportClasses(AdditiveUnit)
33 36
 export(AdditiveUnit)
34 37
 exportMethods(set_component_height)
35 38
 exportMethods(make_column_cluster)
36
-export(HeatmapList)
37 39
 exportMethods(draw_annotation)
40
+export(HeatmapList)
38 41
 export(SingleAnnotation)
39 42
 exportMethods(annotation_legend_size)
40 43
 exportMethods(get_color_mapping_list)
... ...
@@ -43,7 +46,9 @@ export(AdditiveUnit)
43 46
 exportMethods(component_width)
44 47
 exportMethods(draw_heatmap_list)
45 48
 export(grid.dendrogram)
49
+export(decorate_heatmap_body)
46 50
 export(dist2)
51
+export(decorate_title)
47 52
 export(rowAnnotation)
48 53
 exportMethods(draw_heatmap_legend)
49 54
 exportMethods(add_heatmap)
... ...
@@ -1,3 +1,9 @@
1
+CHANGES in VERSION 1.2.8
2
+
3
+* add decorate* functions
4
+
5
+=========================================
6
+
1 7
 CHANGES in VERSION 1.2.7
2 8
 
3 9
 * add `rows_reorder_weight` and `columns_reorder_weight` in `Heatmap()`
4 10
new file mode 100644
... ...
@@ -0,0 +1,193 @@
1
+# == title
2
+# Decorate the heatmap body
3
+#
4
+# == param
5
+# -heatmap name of the heatmap
6
+# -code code that is executed in the heatmap body
7
+# -slice index of row slices in the heatmap
8
+#
9
+# == details
10
+# This simple function actually contructs the name of the viewport,
11
+# goes to the viewport by `grid::seekViewport` and applies code
12
+# to that viewport.
13
+#
14
+# == author
15
+# Zuguang Gu <z.gu@dkfz.de>
16
+#
17
+# == example
18
+# set.seed(123)
19
+# Heatmap(matrix(rnorm(100), 10), name = "mat")
20
+# decorate_heatmap_body("mat", {
21
+#     grid.circle(gp = gpar(fill = "#FF000080"))
22
+# })
23
+#
24
+decorate_heatmap_body = function(heatmap, code = {}, slice = 1) {
25
+
26
+	vp_name = paste0(heatmap, "_heatmap_body_", slice)
27
+
28
+	seekViewport(vp_name)
29
+	e = new.env(parent = parent.frame())
30
+	eval(substitute(code), envir = e)
31
+}
32
+
33
+# == title
34
+# Decorate the heatmap dendrogram
35
+#
36
+# == param
37
+# -heatmap name of the heatmap
38
+# -code code that is executed in the heatmap body
39
+# -slice index of row slices in the heatmap
40
+# -which on rows or on columns?
41
+#
42
+# == details
43
+# This simple function actually contructs the name of the viewport,
44
+# goes to the viewport by `grid::seekViewport` and applies code
45
+# to that viewport.
46
+#
47
+# == author
48
+# Zuguang Gu <z.gu@dkfz.de>
49
+#
50
+# == example
51
+# set.seed(123)
52
+# Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2)
53
+# decorate_hclust("mat", {
54
+#     grid.rect(gp = gpar(fill = "#FF000080"))
55
+# }, which = "row", slice = 2)
56
+#
57
+decorate_hclust = function(heatmap, code, slice = 1, which = c("column", "row")) {
58
+	
59
+	which = match.arg(which)[1]
60
+	if(which == "column") {
61
+		vp_name = paste0(heatmap, "_hclust_", which)
62
+	} else if(which == "row") {
63
+		vp_name = paste0(heatmap, "_hclust_", which, "_", slice)
64
+	}
65
+
66
+	seekViewport(vp_name)
67
+	e = new.env(parent = parent.frame())
68
+	eval(substitute(code), envir = e)
69
+}
70
+
71
+# == title
72
+# Decorate the heatmap dimension names
73
+#
74
+# == param
75
+# -heatmap name of the heatmap
76
+# -code code that is executed in the heatmap body
77
+# -slice index of row slices in the heatmap
78
+# -which on rows or on columns?
79
+#
80
+# == details
81
+# This simple function actually contructs the name of the viewport,
82
+# goes to the viewport by `grid::seekViewport` and applies code
83
+# to that viewport.
84
+#
85
+# == author
86
+# Zuguang Gu <z.gu@dkfz.de>
87
+#
88
+# == example
89
+# set.seed(123)
90
+# mat = matrix(rnorm(100), 10)
91
+# rownames(mat) = letters[1:10]
92
+# colnames(mat) = LETTERS[1:10]
93
+# Heatmap(mat, name = "mat", km = 2)
94
+# 
95
+# decorate_dimnames("mat", {
96
+#     grid.rect(gp = gpar(fill = "#FF000080"))
97
+# }, which = "row", slice = 2)
98
+#
99
+decorate_dimnames = function(heatmap, code, slice = 1, which = c("column", "row")) {
100
+	
101
+	which = match.arg(which)[1]
102
+	if(which == "column") {
103
+		vp_name = paste0(heatmap, "_", which, "_names")
104
+	} else if(which == "row") {
105
+		vp_name = paste0(heatmap, "_", which, "_names_", slice)
106
+	}
107
+
108
+	seekViewport(vp_name)
109
+	e = new.env(parent = parent.frame())
110
+	eval(substitute(code), envir = e)
111
+}
112
+
113
+# == title
114
+# Decorate the heatmap title
115
+#
116
+# == param
117
+# -heatmap name of the heatmap
118
+# -code code that is executed in the heatmap body
119
+# -slice index of row slices in the heatmap
120
+# -which on rows or on columns?
121
+#
122
+# == details
123
+# This simple function actually contructs the name of the viewport,
124
+# goes to the viewport by `grid::seekViewport` and applies code
125
+# to that viewport.
126
+#
127
+# == author
128
+# Zuguang Gu <z.gu@dkfz.de>
129
+#
130
+# == example
131
+# set.seed(123)
132
+# Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2)
133
+# decorate_title("mat", {
134
+#     grid.rect(gp = gpar(fill = "#FF000080"))
135
+# }, which = "row", slice = 2)
136
+#
137
+decorate_title = function(heatmap, code, slice = 1, which = c("column", "row")) {
138
+	
139
+	which = match.arg(which)[1]
140
+	if(which == "column") {
141
+		vp_name = paste0(heatmap, "_", which, "_title")
142
+	} else if(which == "row") {
143
+		vp_name = paste0(heatmap, "_", which, "_title_", slice)
144
+	}
145
+
146
+	seekViewport(vp_name)
147
+	e = new.env(parent = parent.frame())
148
+	eval(substitute(code), envir = e)
149
+}
150
+
151
+# == title
152
+# Decorate the heatmap annotation
153
+#
154
+# == param
155
+# -annotation name of the annotation
156
+# -code code that is executed in the heatmap body
157
+# -slice index of row slices in the heatmap
158
+#
159
+# == details
160
+# This simple function actually contructs the name of the viewport,
161
+# goes to the viewport by `grid::seekViewport` and applies code
162
+# to that viewport.
163
+#
164
+# == author
165
+# Zuguang Gu <z.gu@dkfz.de>
166
+#
167
+# == example
168
+# set.seed(123)
169
+# ha1 = HeatmapAnnotation(df = data.frame(type = rep(letters[1:2], 5)))
170
+# ha2 = rowAnnotation(point = anno_points(runif(10), which = "row"))
171
+# Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2,
172
+#     top_annotation = ha1) + ha2
173
+# decorate_annotation("type", {
174
+#     grid.circle(x = unit(c(0.2, 0.4, 0.6, 0.8), "npc"), 
175
+#         gp = gpar(fill = "#FF000080"))
176
+# })
177
+# decorate_annotation("point", {
178
+#     grid.rect(gp = gpar(fill = "#FF000080"))
179
+# }, slice = 2)
180
+#
181
+decorate_annotation = function(annotation, code, slice = NULL) {
182
+
183
+	if(is.null(slice)) {
184
+		vp_name = paste0("annotation_", annotation)
185
+	} else {
186
+		vp_name = paste0("annotation_", annotation, "_", slice)
187
+	}
188
+
189
+	seekViewport(vp_name)
190
+	e = new.env(parent = parent.frame())
191
+	eval(substitute(code), envir = e)
192
+}
193
+
... ...
@@ -1,4 +0,0 @@
1
-adjust a list of heatmap should only be process through functions 
2
-while not by manually adjustment. (for the prepare-Heatmap method)
3
-
4
-
... ...
@@ -7,7 +7,7 @@ cm = ColorMapping(name = "test",
7 7
 test_that("color mapping is discrete", {
8 8
 	expect_that(show(cm), prints_text("Discrete color mapping"))
9 9
 	expect_that(map_to_colors(cm, "a"), is_identical_to("blue"))
10
-	expect_that(map_to_colors(cm, "d"), throws_error("Cannot map some of the levels"))
10
+	expect_that(map_to_colors(cm, "d"), throws_error("cannot map some of the levels"))
11 11
 	expect_that(map_to_colors(cm, c("a", "a", "b", "c")), is_identical_to(c("blue", "blue", "white", "red")))
12 12
 })
13 13
 
... ...
@@ -29,7 +29,7 @@ test_that("color mapping is discrete but with numeric levels", {
29 29
 	expect_that(show(cm), prints_text("Discrete color mapping"))
30 30
 	expect_that(map_to_colors(cm, 1), is_identical_to("blue"))
31 31
 	expect_that(map_to_colors(cm, "1"), is_identical_to("blue"))
32
-	expect_that(map_to_colors(cm, 5), throws_error("Cannot map some of the levels"))
32
+	expect_that(map_to_colors(cm, 5), throws_error("cannot map some of the levels"))
33 33
 	expect_that(map_to_colors(cm, c(1, 1, 2, 2)), is_identical_to(c("blue", "blue", "white", "white")))
34 34
 })
35 35
 
... ...
@@ -17,5 +17,9 @@ It is only designed for \code{+} generic method so that above three classes can
17 17
 \examples{
18 18
 
19 19
 
20
+
21
+
22
+
23
+
20 24
 # no example
21 25
 NULL}
... ...
@@ -29,5 +29,9 @@ Zuguang Gu <z.gu@dkfz.de>
29 29
 \examples{
30 30
 
31 31
 
32
+
33
+
34
+
35
+
32 36
 # no example
33 37
 NULL}
... ...
@@ -32,5 +32,9 @@ Zuguang Gu <z.gu@dkfz.de>
32 32
 \examples{
33 33
 
34 34
 
35
+
36
+
37
+
38
+
35 39
 # for examples, please go to `ColorMapping` method page
36 40
 NULL}
... ...
@@ -40,6 +40,10 @@ Zuguang Gu <z.gu@dkfz.de>
40 40
 \examples{
41 41
 
42 42
 
43
+
44
+
45
+
46
+
43 47
 # discrete color mapping for characters
44 48
 cm = ColorMapping(name = "test",
45 49
     colors = c("blue", "white", "red"),
... ...
@@ -74,5 +74,9 @@ Zuguang Gu <z.gu@dkfz.de>
74 74
 \examples{
75 75
 
76 76
 
77
+
78
+
79
+
80
+
77 81
 # for examples, please go to `Heatmap` method page
78 82
 NULL}
... ...
@@ -124,6 +124,10 @@ Zuguang Gu <z.gu@dkfz.de>
124 124
 \examples{
125 125
 
126 126
 
127
+
128
+
129
+
130
+
127 131
 mat = matrix(rnorm(80, 2), 8, 10)
128 132
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
129 133
 rownames(mat) = letters[1:12]
... ...
@@ -31,5 +31,9 @@ Zuguang Gu <z.gu@dkfz.de>
31 31
 \examples{
32 32
 
33 33
 
34
+
35
+
36
+
37
+
34 38
 # for examples, please go to `HeatmapAnnotation` method page
35 39
 NULL}
... ...
@@ -50,6 +50,10 @@ Zuguang Gu <z.gu@dkfz.de>
50 50
 \examples{
51 51
 
52 52
 
53
+
54
+
55
+
56
+
53 57
 df = data.frame(type = c("a", "a", "a", "b", "b", "b"))
54 58
 ha = HeatmapAnnotation(df = df)
55 59
 
... ...
@@ -70,6 +70,10 @@ Zuguang Gu <z.gu@dkfz.de>
70 70
 \examples{
71 71
 
72 72
 
73
+
74
+
75
+
76
+
73 77
 mat = matrix(rnorm(80, 2), 8, 10)
74 78
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
75 79
 rownames(mat) = letters[1:12]
... ...
@@ -29,9 +29,17 @@ Zuguang Gu <z.gu@dkfz.de>
29 29
 \section{Detailes}{
30 30
 
31 31
 
32
+
33
+
34
+
35
+
32 36
 There is no public constructor method for the \code{\link{HeatmapList-class}}.}
33 37
 \examples{
34 38
 
35 39
 
40
+
41
+
42
+
43
+
36 44
 # no example
37 45
 NULL}
... ...
@@ -41,5 +41,9 @@ Zuguang Gu <z.gu@dkfz.de>
41 41
 \examples{
42 42
 
43 43
 
44
+
45
+
46
+
47
+
44 48
 # for examples, please go to `SingleAnnotation` method page
45 49
 NULL}
... ...
@@ -51,6 +51,10 @@ Zuguang Gu <z.gu@dkfz.de>
51 51
 \examples{
52 52
 
53 53
 
54
+
55
+
56
+
57
+
54 58
 # discrete character
55 59
 SingleAnnotation(name = "test", value = c("a", "a", "a", "b", "b", "b"))
56 60
 SingleAnnotation(name = "test", value = c("a", "a", "a", "b", "b", "b"), 
... ...
@@ -33,6 +33,10 @@ Zuguang Gu <z.gu@dkfz.de>
33 33
 \examples{
34 34
 
35 35
 
36
+
37
+
38
+
39
+
36 40
 mat = matrix(rnorm(80, 2), 8, 10)
37 41
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
38 42
 rownames(mat) = letters[1:12]
... ...
@@ -30,6 +30,10 @@ Zuguang Gu <z.gu@dkfz.de>
30 30
 \examples{
31 31
 
32 32
 
33
+
34
+
35
+
36
+
33 37
 mat = matrix(rnorm(80, 2), 8, 10)
34 38
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
35 39
 rownames(mat) = letters[1:12]
... ...
@@ -30,6 +30,10 @@ Zuguang Gu <z.gu@dkfz.de>
30 30
 \examples{
31 31
 
32 32
 
33
+
34
+
35
+
36
+
33 37
 mat = matrix(rnorm(80, 2), 8, 10)
34 38
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
35 39
 rownames(mat) = letters[1:12]
... ...
@@ -30,6 +30,10 @@ Zuguang Gu <z.gu@dkfz.de>
30 30
 \examples{
31 31
 
32 32
 
33
+
34
+
35
+
36
+
33 37
 mat = matrix(rnorm(80, 2), 8, 10)
34 38
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
35 39
 rownames(mat) = letters[1:12]
... ...
@@ -33,6 +33,10 @@ Zuguang Gu <z.gu@dkfz.de>
33 33
 \examples{
34 34
 
35 35
 
36
+
37
+
38
+
39
+
36 40
 f = anno_barplot(rnorm(10))
37 41
 grid.newpage(); f(1:10)
38 42
 
... ...
@@ -34,6 +34,10 @@ Zuguang Gu <z.gu@dkfz.de>
34 34
 \examples{
35 35
 
36 36
 
37
+
38
+
39
+
40
+
37 41
 mat = matrix(rnorm(32), nrow = 4)
38 42
 f = anno_boxplot(mat)
39 43
 grid.newpage(); f(1:8)
... ...
@@ -30,6 +30,10 @@ Zuguang Gu <z.gu@dkfz.de>
30 30
 \examples{
31 31
 
32 32
 
33
+
34
+
35
+
36
+
33 37
 mat = matrix(rnorm(32), nrow = 4)
34 38
 f = anno_density(mat)
35 39
 grid.newpage(); f(1:8)
... ...
@@ -28,6 +28,10 @@ Zuguang Gu <z.gu@dkfz.de>
28 28
 \examples{
29 29
 
30 30
 
31
+
32
+
33
+
34
+
31 35
 mat = matrix(rnorm(32), nrow = 4)
32 36
 f = anno_histogram(mat)
33 37
 grid.newpage(); f(1:8)
... ...
@@ -35,5 +35,9 @@ Zuguang Gu <z.gu@dkfz.de>
35 35
 \examples{
36 36
 
37 37
 
38
+
39
+
40
+
41
+
38 42
 f = anno_points(rnorm(10))
39 43
 grid.newpage(); f(1:10)}
... ...
@@ -31,6 +31,10 @@ Zuguang Gu <z.gu@dkfz.de>
31 31
 \examples{
32 32
 
33 33
 
34
+
35
+
36
+
37
+
34 38
 mat = matrix(rnorm(100), 10)
35 39
 colnames(mat) = letters[1:10]
36 40
 rownames(mat) = LETTERS[1:10]
... ...
@@ -35,5 +35,9 @@ Zuguang Gu <z.gu@dkfz.de>
35 35
 \examples{
36 36
 
37 37
 
38
+
39
+
40
+
41
+
38 42
 # no example for this internal method
39 43
 NULL}
... ...
@@ -40,6 +40,10 @@ Zuguang Gu <z.gu@dkfz.de>
40 40
 \examples{
41 41
 
42 42
 
43
+
44
+
45
+
46
+
43 47
 # discrete color mapping for characters
44 48
 cm = ColorMapping(name = "test",
45 49
     colors = c("blue", "white", "red"),
... ...
@@ -33,5 +33,9 @@ Zuguang Gu <z.gu@dkfz.de>
33 33
 \examples{
34 34
 
35 35
 
36
+
37
+
38
+
39
+
36 40
 df = data.frame(type = c("a", "a", "a", "b", "b", "b"))
37 41
 ha = rowAnnotation(df = df)}
... ...
@@ -30,4 +30,8 @@ Zuguang Gu <z.gu@dkfz.de>
30 30
 \examples{
31 31
 
32 32
 
33
+
34
+
35
+
36
+
33 37
 # no example for this internal method}
... ...
@@ -26,4 +26,8 @@ Zuguang Gu <z.gu@dkfz.de>
26 26
 \examples{
27 27
 
28 28
 
29
+
30
+
31
+
32
+
29 33
 # no example for this internal method}
... ...
@@ -22,6 +22,10 @@ This function is only for internal use.
22 22
 \section{Detials}{
23 23
 
24 24
 
25
+
26
+
27
+
28
+
25 29
 This function is only for internal use.}
26 30
 \value{
27 31
 A \code{\link[grid]{unit}} object.
... ...
@@ -34,4 +38,8 @@ Zuguang Gu <z.gu@dkfz.de>
34 38
 \examples{
35 39
 
36 40
 
41
+
42
+
43
+
44
+
37 45
 # no example for this internal method}
... ...
@@ -30,4 +30,8 @@ Zuguang Gu <z.gu@dkfz.de>
30 30
 \examples{
31 31
 
32 32
 
33
+
34
+
35
+
36
+
33 37
 # no example for this internal method}
34 38
new file mode 100644
... ...
@@ -0,0 +1,43 @@
1
+\name{decorate_annotation}
2
+\alias{decorate_annotation}
3
+\title{
4
+Decorate the heatmap annotation
5
+
6
+}
7
+\description{
8
+Decorate the heatmap annotation
9
+
10
+}
11
+\usage{
12
+decorate_annotation(annotation, code, slice = NULL)}
13
+\arguments{
14
+
15
+  \item{annotation}{name of the annotation}
16
+  \item{code}{code that is executed in the heatmap body}
17
+  \item{slice}{index of row slices in the heatmap}
18
+}
19
+\details{
20
+This simple function actually contructs the name of the viewport,
21
+goes to the viewport by \code{\link[grid]{seekViewport}} and applies code
22
+to that viewport.
23
+
24
+}
25
+\author{
26
+Zuguang Gu <z.gu@dkfz.de>
27
+
28
+}
29
+\examples{
30
+set.seed(123)
31
+ha1 = HeatmapAnnotation(df = data.frame(type = rep(letters[1:2], 5)))
32
+ha2 = rowAnnotation(point = anno_points(runif(10), which = "row"))
33
+Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2,
34
+    top_annotation = ha1) + ha2
35
+decorate_annotation("type", {
36
+    grid.circle(x = unit(c(0.2, 0.4, 0.6, 0.8), "npc"), 
37
+        gp = gpar(fill = "#FF000080"))
38
+})
39
+decorate_annotation("point", {
40
+    grid.rect(gp = gpar(fill = "#FF000080"))
41
+}, slice = 2)
42
+
43
+}
0 44
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+\name{decorate_dimnames}
2
+\alias{decorate_dimnames}
3
+\title{
4
+Decorate the heatmap dimension names
5
+
6
+}
7
+\description{
8
+Decorate the heatmap dimension names
9
+
10
+}
11
+\usage{
12
+decorate_dimnames(heatmap, code, slice = 1, which = c("column", "row"))}
13
+\arguments{
14
+
15
+  \item{heatmap}{name of the heatmap}
16
+  \item{code}{code that is executed in the heatmap body}
17
+  \item{slice}{index of row slices in the heatmap}
18
+  \item{which}{on rows or on columns?}
19
+}
20
+\details{
21
+This simple function actually contructs the name of the viewport,
22
+goes to the viewport by \code{\link[grid]{seekViewport}} and applies code
23
+to that viewport.
24
+
25
+}
26
+\author{
27
+Zuguang Gu <z.gu@dkfz.de>
28
+
29
+}
30
+\examples{
31
+set.seed(123)
32
+mat = matrix(rnorm(100), 10)
33
+rownames(mat) = letters[1:10]
34
+colnames(mat) = LETTERS[1:10]
35
+Heatmap(mat, name = "mat", km = 2)
36
+
37
+decorate_dimnames("mat", {
38
+    grid.rect(gp = gpar(fill = "#FF000080"))
39
+}, which = "row", slice = 2)
40
+
41
+}
0 42
new file mode 100644
... ...
@@ -0,0 +1,37 @@
1
+\name{decorate_hclust}
2
+\alias{decorate_hclust}
3
+\title{
4
+Decorate the heatmap dendrogram
5
+
6
+}
7
+\description{
8
+Decorate the heatmap dendrogram
9
+
10
+}
11
+\usage{
12
+decorate_hclust(heatmap, code, slice = 1, which = c("column", "row"))}
13
+\arguments{
14
+
15
+  \item{heatmap}{name of the heatmap}
16
+  \item{code}{code that is executed in the heatmap body}
17
+  \item{slice}{index of row slices in the heatmap}
18
+  \item{which}{on rows or on columns?}
19
+}
20
+\details{
21
+This simple function actually contructs the name of the viewport,
22
+goes to the viewport by \code{\link[grid]{seekViewport}} and applies code
23
+to that viewport.
24
+
25
+}
26
+\author{
27
+Zuguang Gu <z.gu@dkfz.de>
28
+
29
+}
30
+\examples{
31
+set.seed(123)
32
+Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2)
33
+decorate_hclust("mat", {
34
+    grid.rect(gp = gpar(fill = "#FF000080"))
35
+}, which = "row", slice = 2)
36
+
37
+}
0 38
new file mode 100644
... ...
@@ -0,0 +1,36 @@
1
+\name{decorate_heatmap_body}
2
+\alias{decorate_heatmap_body}
3
+\title{
4
+Decorate the heatmap body
5
+
6
+}
7
+\description{
8
+Decorate the heatmap body
9
+
10
+}
11
+\usage{
12
+decorate_heatmap_body(heatmap, code = {}, slice = 1)}
13
+\arguments{
14
+
15
+  \item{heatmap}{name of the heatmap}
16
+  \item{code}{code that is executed in the heatmap body}
17
+  \item{slice}{index of row slices in the heatmap}
18
+}
19
+\details{
20
+This simple function actually contructs the name of the viewport,
21
+goes to the viewport by \code{\link[grid]{seekViewport}} and applies code
22
+to that viewport.
23
+
24
+}
25
+\author{
26
+Zuguang Gu <z.gu@dkfz.de>
27
+
28
+}
29
+\examples{
30
+set.seed(123)
31
+Heatmap(matrix(rnorm(100), 10), name = "mat")
32
+decorate_heatmap_body("mat", {
33
+    grid.circle(gp = gpar(fill = "#FF000080"))
34
+})
35
+
36
+}
0 37
new file mode 100644
... ...
@@ -0,0 +1,37 @@
1
+\name{decorate_title}
2
+\alias{decorate_title}
3
+\title{
4
+Decorate the heatmap title
5
+
6
+}
7
+\description{
8
+Decorate the heatmap title
9
+
10
+}
11
+\usage{
12
+decorate_title(heatmap, code, slice = 1, which = c("column", "row"))}
13
+\arguments{
14
+
15
+  \item{heatmap}{name of the heatmap}
16
+  \item{code}{code that is executed in the heatmap body}
17
+  \item{slice}{index of row slices in the heatmap}
18
+  \item{which}{on rows or on columns?}
19
+}
20
+\details{
21
+This simple function actually contructs the name of the viewport,
22
+goes to the viewport by \code{\link[grid]{seekViewport}} and applies code
23
+to that viewport.
24
+
25
+}
26
+\author{
27
+Zuguang Gu <z.gu@dkfz.de>
28
+
29
+}
30
+\examples{
31
+set.seed(123)
32
+Heatmap(matrix(rnorm(100), 10), name = "mat", km = 2)
33
+decorate_title("mat", {
34
+    grid.rect(gp = gpar(fill = "#FF000080"))
35
+}, which = "row", slice = 2)
36
+
37
+}
... ...
@@ -32,6 +32,10 @@ Zuguang Gu <z.gu@dkfz.de>
32 32
 \examples{
33 33
 
34 34
 
35
+
36
+
37
+
38
+
35 39
 mat = matrix(rnorm(40), nr = 4, ncol = 10)
36 40
 rownames(mat) = letters[1:4]
37 41
 colnames(mat) = letters[1:10]
... ...
@@ -33,6 +33,10 @@ Zuguang Gu <z.gu@dkfz.de>
33 33
 \examples{
34 34
 
35 35
 
36
+
37
+
38
+
39
+
36 40
 mat = matrix(rnorm(80, 2), 8, 10)
37 41
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
38 42
 rownames(mat) = letters[1:12]
... ...
@@ -33,6 +33,10 @@ Zuguang Gu <z.gu@dkfz.de>
33 33
 \examples{
34 34
 
35 35
 
36
+
37
+
38
+
39
+
36 40
 df = data.frame(type = c("a", "a", "a", "b", "b", "b"))
37 41
 ha = HeatmapAnnotation(df = df)
38 42
 grid.newpage(); draw(ha, 1:6)
... ...
@@ -36,6 +36,10 @@ Zuguang Gu <z.gu@dkfz.de>
36 36
 \examples{
37 37
 
38 38
 
39
+
40
+
41
+
42
+
39 43
 mat = matrix(rnorm(80, 2), 8, 10)
40 44
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
41 45
 rownames(mat) = letters[1:12]
... ...
@@ -34,6 +34,10 @@ Zuguang Gu <z.gu@dkfz.de>
34 34
 \examples{
35 35
 
36 36
 
37
+
38
+
39
+
40
+
37 41
 anno = SingleAnnotation(name = "test", value = c("a", "a", "a", "b", "b", "b"))
38 42
 grid.newpage(); draw(anno, 1:5)
39 43
 grid.newpage(); draw(anno, c(1, 4, 3, 5, 2))
... ...
@@ -36,5 +36,9 @@ Zuguang Gu <z.gu@dkfz.de>
36 36
 \examples{
37 37
 
38 38
 
39
+
40
+
41
+
42
+
39 43
 # no example for this internal method
40 44
 NULL}
... ...
@@ -34,5 +34,9 @@ Zuguang Gu <z.gu@dkfz.de>
34 34
 \examples{
35 35
 
36 36
 
37
+
38
+
39
+
40
+
37 41
 # no example for this internal method
38 42
 NULL}
... ...
@@ -37,5 +37,9 @@ Zuguang Gu <z.gu@dkfz.de>
37 37
 \examples{
38 38
 
39 39
 
40
+
41
+
42
+
43
+
40 44
 # no example for this internal method
41 45
 NULL}
... ...
@@ -44,5 +44,9 @@ Zuguang Gu <z.gu@dkfz.de>
44 44
 \examples{
45 45
 
46 46
 
47
+
48
+
49
+
50
+
47 51
 # no example for this internal method
48 52
 NULL}
... ...
@@ -38,5 +38,9 @@ Zuguang Gu <z.gu@dkfz.de>
38 38
 \examples{
39 39
 
40 40
 
41
+
42
+
43
+
44
+
41 45
 # no example for this internal method
42 46
 NULL}
... ...
@@ -34,5 +34,9 @@ Zuguang Gu <z.gu@dkfz.de>
34 34
 \examples{
35 35
 
36 36
 
37
+
38
+
39
+
40
+
37 41
 # no example for this internal method
38 42
 NULL}
... ...
@@ -32,5 +32,9 @@ Zuguang Gu <z.gu@dkfz.de>
32 32
 \examples{
33 33
 
34 34
 
35
+
36
+
37
+
38
+
35 39
 # no example for this internal method
36 40
 NULL}
... ...
@@ -35,5 +35,9 @@ Zuguang Gu <z.gu@dkfz.de>
35 35
 \examples{
36 36
 
37 37
 
38
+
39
+
40
+
41
+
38 42
 # no example for this internal method
39 43
 NULL}
... ...
@@ -33,5 +33,9 @@ Zuguang Gu <z.gu@dkfz.de>
33 33
 \examples{
34 34
 
35 35
 
36
+
37
+
38
+
39
+
36 40
 # no example for this internal method
37 41
 NULL}
... ...
@@ -32,5 +32,9 @@ Zuguang Gu <z.gu@dkfz.de>
32 32
 \examples{
33 33
 
34 34
 
35
+
36
+
37
+
38
+
35 39
 # no example for this internal method
36 40
 NULL}
... ...
@@ -36,6 +36,10 @@ Zuguang Gu <z.gu@dkfz.de>
36 36
 \examples{
37 37
 
38 38
 
39
+
40
+
41
+
42
+
39 43
 hc = hclust(dist(USArrests[1:5, ]))
40 44
 dend = as.dendrogram(hc)
41 45
 
... ...
@@ -32,5 +32,9 @@ Zuguang Gu <z.gu@dkfz.de>
32 32
 \examples{
33 33
 
34 34
 
35
+
36
+
37
+
38
+
35 39
 # no example for this internal method
36 40
 NULL}
... ...
@@ -32,5 +32,9 @@ Zuguang Gu <z.gu@dkfz.de>
32 32
 \examples{
33 33
 
34 34
 
35
+
36
+
37
+
38
+
35 39
 # no example for this internal method
36 40
 NULL}
... ...
@@ -35,5 +35,9 @@ Zuguang Gu <z.gu@dkfz.de>
35 35
 \examples{
36 36
 
37 37
 
38
+
39
+
40
+
41
+
38 42
 # no example for this internal method
39 43
 NULL}
... ...
@@ -61,5 +61,9 @@ Zuguang Gu <z.gu@dkfz.de>
61 61
 \examples{
62 62
 
63 63
 
64
+
65
+
66
+
67
+
64 68
 # no example for this internal method
65 69
 NULL}
... ...
@@ -34,5 +34,9 @@ Zuguang Gu <z.gu@dkfz.de>
34 34
 \examples{
35 35
 
36 36
 
37
+
38
+
39
+
40
+
37 41
 # no example for this internal method
38 42
 NULL}
... ...
@@ -31,6 +31,10 @@ Zuguang Gu <z.gu@dkfz.de>
31 31
 \examples{
32 32
 
33 33
 
34
+
35
+
36
+
37
+
34 38
 # discrete color mapping for characters
35 39
 cm = ColorMapping(name = "test",
36 40
     colors = c("blue", "white", "red"),
... ...
@@ -49,6 +49,10 @@ Zuguang Gu <z.gu@dkfz.de>
49 49
 \examples{
50 50
 
51 51
 
52
+
53
+
54
+
55
+
52 56
 df = data.frame(matrix(rnorm(40), nrow = 10, dimnames = list(letters[1:10], letters[1:4])),
53 57
                 large = runif(10)*100,
54 58
                 t1 = sample(letters[1:3], 10, replace = TRUE),
... ...
@@ -39,5 +39,9 @@ Zuguang Gu <z.gu@dkfz.de>
39 39
 \examples{
40 40
 
41 41
 
42
+
43
+
44
+
45
+
42 46
 # no example for this internal method
43 47
 NULL}
... ...
@@ -33,5 +33,9 @@ Zuguang Gu <z.gu@dkfz.de>
33 33
 \examples{
34 34
 
35 35
 
36
+
37
+
38
+
39
+
36 40
 df = data.frame(type = c("a", "a", "a", "b", "b", "b"))
37 41
 ha = columnAnnotation(df = df)}
... ...
@@ -32,5 +32,9 @@ Zuguang Gu <z.gu@dkfz.de>
32 32
 \examples{
33 33
 
34 34
 
35
+
36
+
37
+
38
+
35 39
 # no example for this internal method
36 40
 NULL}
... ...
@@ -30,6 +30,10 @@ Zuguang Gu <z.gu@dkfz.de>
30 30
 \examples{
31 31
 
32 32
 
33
+
34
+
35
+
36
+
33 37
 mat = matrix(rnorm(80, 2), 8, 10)
34 38
 mat = rbind(mat, matrix(rnorm(40, -2), 4, 10))
35 39
 rownames(mat) = letters[1:12]
... ...
@@ -26,4 +26,8 @@ This function returns no value.
26 26
 \author{
27 27
 
28 28
 
29
+
30
+
31
+
32
+
29 33
 Zuguang Gu <z.gu@dkfz.de>}
... ...
@@ -939,9 +939,11 @@ Heatmap(mat) + Heatmap(df, name = "type", col = c("a" = "red", "b" = "blue"),
939 939
     width = unit(1, "cm"))
940 940
 ```
941 941
 
942
-## Access components
942
+## Decorate heatmaps and annotations
943
+
944
+Each components of the heatmap/heatmap list has a name (unique id). You can go to any viewport 
945
+to add graphics in by specifying the heatmap name.
943 946
 
944
-Each components of the heatmap/heatmap list has a name (unique id). You can go to any viewport by `seekViewport()`.
945 947
 First generate a figure that almost contains all types of heatmap components.
946 948
 
947 949
 ```{r access_components, fig.width = 10, fig.height = 7, echo = 1:8}
... ...
@@ -1001,7 +1003,7 @@ The components that have names are:
1001 1003
 - `global_row_title`: the viewport which contains row title for the heatmap list.
1002 1004
 - `main_heatmap_list`: the viewport which contains a list of heatmaps and row annotations.
1003 1005
 - `heatmap_@{heatmap_name}`: the viewport which contains a single heatmap
1004
-- `annotation_@{annotation_name}`: the viewport which contains an annotation either on columns or rows.
1006
+- `annotation_@{annotation_name}`: the viewport which contains an annotation on columns.
1005 1007
 - `annotation_@{annotation_name}_@{i}`: for row annotations
1006 1008
 - `@{heatmap_name}_heatmap_body_@{i}`: the heatmap body.
1007 1009
 - `@{heatmap_name}_column_title`: column title for a single heatmap.
... ...
@@ -1015,30 +1017,42 @@ The components that have names are:
1015 1017
 - `annotation_legend`: the viewport which contains all annotation legends.
1016 1018
 - `legend_@{annotation_name}`: the viewport which contains a single annotation legend.
1017 1019
 
1020
+You can easily go to these component and add graphics by `decorate_*` family functions.
1021
+
1018 1022
 Following code add annotation names, mark one grid in the heatmap and seperate the first column clusters with two rectangles.
1019 1023
 
1020 1024
 ```{r, fig.width = 10, fig.height = 7}
1021 1025
 dend_list = draw(ht_list, row_title = "Heatmap list", column_title = "Heatmap list", 
1022 1026
     heatmap_legend_side = "right", annotation_legend_side = "left")
1023
-seekViewport("annotation_points")
1024
-grid.text("points", unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right")
1027
+decorate_annotation("points", {
1028
+    grid.text("points", unit(0, "npc") - unit(2, "mm"), 0.5, 
1029
+        default.units = "npc", just = "right")
1030
+})
1031
+
1032
+decorate_heatmap_body("ht1", {
1033
+    grid.text("outlier", 1.5/10, 2.5/4, default.units = "npc")
1034
+    grid.lines(c(0.5, 0.5), c(0, 1), gp = gpar(lty = 2, col = "#EEEEEE"))
1035
+}, slice = 2)
1025 1036
 
1026
-seekViewport("ht1_heatmap_body_2")
1027
-grid.text("outlier", 1.5/10, 2.5/4, default.units = "npc")
1037
+decorate_hclust("ht1", {
1038
+    tree = dend_list$ht1$column
1039
+    ind = cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)]
1028 1040
 
1029
-seekViewport("annotation_type")
1030
-grid.text("type", unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left")
1041
+    first_index = function(l) which(l)[1]
1042
+    last_index = function(l) { x = which(l); x[length(x)] }
1043
+    x1 = c(first_index(ind == 1), first_index(ind == 2)) - 1
1044
+    x2 = c(last_index(ind == 1), last_index(ind == 2))
1045
+    grid.rect(x = x1/length(ind), width = (x2 - x1)/length(ind), just = "left",
1046
+        default.units = "npc", gp = gpar(fill = c("#FF000040", "#00FF0040"), col = NA))
1047
+}, which = "column")
1031 1048
 
1032
-seekViewport("ht1_hclust_column")
1033
-tree = dend_list$ht1$column
1034
-ind = cutree(as.hclust(tree), k = 2)[order.dendrogram(tree)]
1049
+decorate_dimnames("ht2", {
1050
+    grid.rect(gp = gpar(fill = "#FF000040"))
1051
+}, which = "row", slice = 2)
1035 1052
 
1036
-first_index = function(l) which(l)[1]
1037
-last_index = function(l) { x = which(l); x[length(x)] }
1038
-x1 = c(first_index(ind == 1), first_index(ind == 2)) - 1
1039
-x2 = c(last_index(ind == 1), last_index(ind == 2))
1040
-grid.rect(x = x1/length(ind), width = (x2 - x1)/length(ind), just = "left",
1041
-    default.units = "npc", gp = gpar(fill = c("#FF000040", "#00FF0040"), col = NA))
1053
+decorate_title("ht1", {
1054
+    grid.rect(gp = gpar(fill = "#00FF0040"))
1055
+}, which = "row", slice = 1)
1042 1056
 ```
1043 1057
 
1044 1058
 ## Add annotation names
... ...
@@ -1049,7 +1063,7 @@ would makes the adjustment of the heatmap layout difficult. But since you can go
1049 1063
 in the heatmap list by its name, actually it is not difficult to add annotation names manually.
1050 1064
 
1051 1065
 Following code add annotation names on the both sides of the column annotations. The drawback is
1052
-since there is no specific component designed for annotatio names, if the annotation name is too long,
1066
+since there is no specific component designed for annotation names, if the annotation name is too long,
1053 1067
 it will be exceeding the figure region.
1054 1068
 
1055 1069
 ```{r}
... ...
@@ -1059,11 +1073,12 @@ ha = HeatmapAnnotation(df, col = list(type1 = c("a" = "red", "b" = "blue"),
1059 1073
                                       type2 = c("A" = "green", "B" = "orange")))
1060 1074
 Heatmap(mat, name = "ht", top_annotation = ha)
1061 1075
 for(an in colnames(df)) {
1062
-    seekViewport(qq("annotation_@{an}"))
1063
-    # annotation names on the right
1064
-    grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left")
1065
-    # annotation names on the left
1066
-    grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right")
1076
+    decorate_annotation(an, {
1077
+        # annotation names on the right
1078
+        grid.text(an, unit(1, "npc") + unit(2, "mm"), 0.5, default.units = "npc", just = "left")
1079
+        # annotation names on the left
1080
+        grid.text(an, unit(0, "npc") - unit(2, "mm"), 0.5, default.units = "npc", just = "right")
1081
+    })
1067 1082
 }
1068 1083
 ```
1069 1084