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
... | ... |
@@ -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) |
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 |
+ |
... | ... |
@@ -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 |
|
... | ... |
@@ -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} |
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 |
+} |
... | ... |
@@ -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 |
|