Commit id: ff9ffa810ca9f16a253861d97301062962dbe38b
titles support expression now
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ComplexHeatmap@106147 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -1,6 +1,8 @@ |
1 | 1 |
CHANGES in VERSION 1.2.4 |
2 | 2 |
|
3 |
-* legend for continuous values can be set as continuous legends |
|
3 |
+* legend for continuous values can be set as continuous legends |
|
4 |
+* row title and column title as well as legend title support expression |
|
5 |
+* add `heatmap_legend_title` in `Heatmap` and `ColorMapping` |
|
4 | 6 |
|
5 | 7 |
========================================== |
6 | 8 |
|
... | ... |
@@ -29,7 +29,8 @@ ColorMapping = setClass("ColorMapping", |
29 | 29 |
type = "character", # continuous or discrete |
30 | 30 |
name = "character", # used to map to the dataset and taken as the title of the legend |
31 | 31 |
na_col = "character", |
32 |
- color_bar = "character" |
|
32 |
+ color_bar = "character", |
|
33 |
+ legend_title = "ANY" |
|
33 | 34 |
) |
34 | 35 |
) |
35 | 36 |
|
... | ... |
@@ -47,6 +48,7 @@ ColorMapping = setClass("ColorMapping", |
47 | 48 |
# -enforce_breaks If it is set to `TRUE`, values of ``breaks`` will be the final break value in the legend. |
48 | 49 |
# If it is ``FALSE``, proper breaks values will be automatically generated. |
49 | 50 |
# -na_col colors for ``NA`` values. |
51 |
+# -legend_title title of the legend, by default it is the name of the legend |
|
50 | 52 |
# -color_bar if the color mapping is continuous, whether draw the legend discrete or continuous. |
51 | 53 |
# |
52 | 54 |
# == detail |
... | ... |
@@ -61,7 +63,7 @@ ColorMapping = setClass("ColorMapping", |
61 | 63 |
# |
62 | 64 |
ColorMapping = function(name, colors = NULL, levels = NULL, |
63 | 65 |
col_fun = NULL, breaks = NULL, enforce_breaks = FALSE, na_col = "#FFFFFF", |
64 |
- color_bar = c("discrete", "continuous")) { |
|
66 |
+ legend_title = name, color_bar = c("discrete", "continuous")) { |
|
65 | 67 |
|
66 | 68 |
.Object = new("ColorMapping") |
67 | 69 |
|
... | ... |
@@ -116,6 +118,7 @@ ColorMapping = function(name, colors = NULL, levels = NULL, |
116 | 118 |
.Object@name = name |
117 | 119 |
.Object@na_col = na_col |
118 | 120 |
|
121 |
+ .Object@legend_title = legend_title |
|
119 | 122 |
color_bar = match.arg(color_bar)[1] |
120 | 123 |
if(.Object@type == "discrete" && color_bar == "continuous") { |
121 | 124 |
stop("Continuous color bar can only be applied to continuous color mapping.") |
... | ... |
@@ -245,7 +248,7 @@ setMethod(f = "color_mapping_legend", |
245 | 248 |
color_bar = object@color_bar |
246 | 249 |
|
247 | 250 |
# add title |
248 |
- legend_title_grob = textGrob(object@name, just = c("left", "top"), gp = legend_title_gp) |
|
251 |
+ legend_title_grob = textGrob(object@legend_title, just = c("left", "top"), gp = legend_title_gp) |
|
249 | 252 |
legend_title_height = grobHeight(legend_title_grob) |
250 | 253 |
legend_title_width = grobWidth(legend_title_grob) |
251 | 254 |
|
... | ... |
@@ -261,7 +264,7 @@ setMethod(f = "color_mapping_legend", |
261 | 264 |
gf = frameGrob(layout = grid.layout(nr = 2, nc = 2, width = unit.c(legend_grid_width, grid_padding + legend_label_max_width), |
262 | 265 |
height = unit.c(legend_title_height, legend_padding + nlevel*(legend_grid_height)))) |
263 | 266 |
# legend title |
264 |
- gf = packGrob(gf, row = 1, col = 1:2, grob = textGrob(object@name, x = 0, y = 1, default.units = "npc", just = c("left", "top"), gp = legend_title_gp)) |
|
267 |
+ gf = packGrob(gf, row = 1, col = 1:2, grob = textGrob(object@legend_title, x = 0, y = 1, default.units = "npc", just = c("left", "top"), gp = legend_title_gp)) |
|
265 | 268 |
|
266 | 269 |
# legend grid |
267 | 270 |
x = unit(rep(0, nlevel), "npc") |
... | ... |
@@ -68,11 +68,11 @@ Heatmap = setClass("Heatmap", |
68 | 68 |
matrix_param = "list", |
69 | 69 |
matrix_color_mapping = "ANY", |
70 | 70 |
|
71 |
- row_title = "character", |
|
71 |
+ row_title = "ANY", |
|
72 | 72 |
row_title_rot = "numeric", |
73 | 73 |
row_title_just = "numeric", |
74 | 74 |
row_title_param = "list", |
75 |
- column_title = "character", |
|
75 |
+ column_title = "ANY", |
|
76 | 76 |
column_title_param = "list", |
77 | 77 |
column_title_rot = "numeric", |
78 | 78 |
column_title_just = "numeric", |
... | ... |
@@ -178,6 +178,7 @@ Heatmap = setClass("Heatmap", |
178 | 178 |
# -width the width of the single heatmap, should be a fixed `grid::unit` object. It is used for the layout when the heatmap |
179 | 179 |
# is appended to a list of heatmaps. |
180 | 180 |
# -show_heatmap_legend whether show heatmap legend? |
181 |
+# -heatmap_legend_title title for the heatmap legend. By default it is the name of the heatmap |
|
181 | 182 |
# -heatmap_legend_color_bar if the matrix is continuous, whether should the legend as continuous color bar as well? |
182 | 183 |
# |
183 | 184 |
# == details |
... | ... |
@@ -225,6 +226,7 @@ Heatmap = function(matrix, col, name, na_col = "grey", rect_gp = gpar(col = NA), |
225 | 226 |
km = 1, split = NULL, gap = unit(1, "mm"), |
226 | 227 |
combined_name_fun = function(x) paste(x, collapse = "/"), |
227 | 228 |
width = NULL, show_heatmap_legend = TRUE, |
229 |
+ heatmap_legend_title = name, |
|
228 | 230 |
heatmap_legend_color_bar = c("discrete", "continuous")) { |
229 | 231 |
|
230 | 232 |
.Object = new("Heatmap") |
... | ... |
@@ -301,31 +303,33 @@ Heatmap = function(matrix, col, name, na_col = "grey", rect_gp = gpar(col = NA), |
301 | 303 |
col = default_col(matrix, main_matrix = TRUE) |
302 | 304 |
} |
303 | 305 |
if(is.function(col)) { |
304 |
- .Object@matrix_color_mapping = ColorMapping(col_fun = col, name = name, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
306 |
+ .Object@matrix_color_mapping = ColorMapping(col_fun = col, name = name, legend_title = heatmap_legend_title, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
305 | 307 |
} else { |
306 | 308 |
if(is.null(names(col))) { |
307 | 309 |
if(length(col) == length(unique(matrix))) { |
308 | 310 |
names(col) = unique(matrix) |
309 |
- .Object@matrix_color_mapping = ColorMapping(colors = col, name = name, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
311 |
+ .Object@matrix_color_mapping = ColorMapping(colors = col, name = name, legend_title = heatmap_legend_title, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
310 | 312 |
} else if(is.numeric(matrix)) { |
311 | 313 |
col = colorRamp2(seq(min(matrix, na.rm = TRUE), max(matrix, na.rm = TRUE), length = length(col)), |
312 | 314 |
col) |
313 |
- .Object@matrix_color_mapping = ColorMapping(col_fun = col, name = name, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
315 |
+ .Object@matrix_color_mapping = ColorMapping(col_fun = col, name = name, legend_title = heatmap_legend_title, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
314 | 316 |
} else { |
315 | 317 |
stop("`col` should have names to map to values in `mat`.") |
316 | 318 |
} |
317 | 319 |
} else { |
318 |
- .Object@matrix_color_mapping = ColorMapping(colors = col, name = name, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
320 |
+ .Object@matrix_color_mapping = ColorMapping(colors = col, name = name, legend_title = heatmap_legend_title, na_col = na_col, color_bar = heatmap_legend_color_bar) |
|
319 | 321 |
} |
320 | 322 |
} |
321 | 323 |
} |
322 | 324 |
|
323 | 325 |
if(length(row_title) == 0) { |
324 | 326 |
row_title = character(0) |
325 |
- } else if(is.na(row_title)) { |
|
326 |
- row_title = character(0) |
|
327 |
- } else if(row_title == "") { |
|
328 |
- row_title = character(0) |
|
327 |
+ } else if(!inherits(row_title, "expression")) { |
|
328 |
+ if(is.na(row_title)) { |
|
329 |
+ row_title = character(0) |
|
330 |
+ } else if(row_title == "") { |
|
331 |
+ row_title = character(0) |
|
332 |
+ } |
|
329 | 333 |
} |
330 | 334 |
.Object@row_title = row_title |
331 | 335 |
.Object@row_title_rot = row_title_rot %% 360 |
... | ... |
@@ -336,10 +340,12 @@ Heatmap = function(matrix, col, name, na_col = "grey", rect_gp = gpar(col = NA), |
336 | 340 |
|
337 | 341 |
if(length(column_title) == 0) { |
338 | 342 |
column_title = character(0) |
339 |
- } else if(is.na(column_title)) { |
|
340 |
- column_title = character(0) |
|
341 |
- } else if(column_title == "") { |
|
342 |
- column_title = character(0) |
|
343 |
+ } else if(!inherits(column_title, "expression")) { |
|
344 |
+ if(is.na(column_title)) { |
|
345 |
+ column_title = character(0) |
|
346 |
+ } else if(column_title == "") { |
|
347 |
+ column_title = character(0) |
|
348 |
+ } |
|
343 | 349 |
} |
344 | 350 |
.Object@column_title = column_title |
345 | 351 |
.Object@column_title_rot = column_title_rot %% 360 |
... | ... |
@@ -54,9 +54,9 @@ HeatmapList = setClass("HeatmapList", |
54 | 54 |
ht_list = "list", |
55 | 55 |
ht_list_param = "list", |
56 | 56 |
|
57 |
- row_title = "character", |
|
57 |
+ row_title = "ANY", |
|
58 | 58 |
row_title_param = "list", |
59 |
- column_title = "character", |
|
59 |
+ column_title = "ANY", |
|
60 | 60 |
column_title_param = "list", |
61 | 61 |
|
62 | 62 |
annotation_legend_param = "list", |
... | ... |
@@ -368,10 +368,12 @@ setMethod(f = "make_layout", |
368 | 368 |
column_title_side = match.arg(column_title_side)[1] |
369 | 369 |
if(length(column_title) == 0) { |
370 | 370 |
column_title = character(0) |
371 |
- } else if(is.na(column_title)) { |
|
372 |
- column_title = character(0) |
|
373 |
- } else if(column_title == "") { |
|
374 |
- column_title = character(0) |
|
371 |
+ } else if(!inherits(column_title, "expression")) { |
|
372 |
+ if(is.na(column_title)) { |
|
373 |
+ column_title = character(0) |
|
374 |
+ } else if(column_title == "") { |
|
375 |
+ column_title = character(0) |
|
376 |
+ } |
|
375 | 377 |
} |
376 | 378 |
object@column_title = column_title |
377 | 379 |
object@column_title_param$gp = check_gp(column_title_gp) |
... | ... |
@@ -392,10 +394,12 @@ setMethod(f = "make_layout", |
392 | 394 |
row_title_side = match.arg(row_title_side)[1] |
393 | 395 |
if(length(row_title) == 0) { |
394 | 396 |
row_title = character(0) |
395 |
- } else if(is.na(row_title)) { |
|
396 |
- row_title = character(0) |
|
397 |
- } else if(row_title == "") { |
|
398 |
- row_title = character(0) |
|
397 |
+ } else if(!inherits(row_title, "expression")) { |
|
398 |
+ if(is.na(row_title)) { |
|
399 |
+ row_title = character(0) |
|
400 |
+ } else if(row_title == "") { |
|
401 |
+ row_title = character(0) |
|
402 |
+ } |
|
399 | 403 |
} |
400 | 404 |
object@row_title = row_title |
401 | 405 |
object@row_title_param$gp = check_gp(row_title_gp) |
... | ... |
@@ -11,7 +11,7 @@ Constructor methods for ColorMapping class |
11 | 11 |
\usage{ |
12 | 12 |
ColorMapping(name, colors = NULL, levels = NULL, |
13 | 13 |
col_fun = NULL, breaks = NULL, enforce_breaks = FALSE, na_col = "#FFFFFF", |
14 |
- color_bar = c("discrete", "continuous"))} |
|
14 |
+ legend_title = name, color_bar = c("discrete", "continuous"))} |
|
15 | 15 |
\arguments{ |
16 | 16 |
|
17 | 17 |
\item{name}{name for this color mapping. It is used for drawing the title of the legend.} |
... | ... |
@@ -21,6 +21,7 @@ ColorMapping(name, colors = NULL, levels = NULL, |
21 | 21 |
\item{breaks}{breaks for the continuous color mapping. If \code{col_fun} isgenerated by \code{\link[circlize]{colorRamp2}}, \code{breaks} can be ignored.} |
22 | 22 |
\item{enforce_breaks}{If it is set to \code{\link{TRUE}}, values of \code{breaks} will be the final break value in the legend.If it is \code{FALSE}, proper breaks values will be automatically generated.} |
23 | 23 |
\item{na_col}{colors for \code{NA} values.} |
24 |
+ \item{legend_title}{title of the legend, by default it is the name of the legend} |
|
24 | 25 |
\item{color_bar}{if the color mapping is continuous, whether draw the legend discrete or continuous.} |
25 | 26 |
} |
26 | 27 |
\details{ |
... | ... |
@@ -37,8 +38,6 @@ Zuguang Gu <z.gu@dkfz.de> |
37 | 38 |
|
38 | 39 |
} |
39 | 40 |
\examples{ |
40 |
- |
|
41 |
- |
|
42 | 41 |
# discrete color mapping for characters |
43 | 42 |
cm = ColorMapping(name = "test", |
44 | 43 |
colors = c("blue", "white", "red"), |
... | ... |
@@ -35,6 +35,7 @@ Heatmap(matrix, col, name, na_col = "grey", rect_gp = gpar(col = NA), |
35 | 35 |
km = 1, split = NULL, gap = unit(1, "mm"), |
36 | 36 |
combined_name_fun = function(x) paste(x, collapse = "/"), |
37 | 37 |
width = NULL, show_heatmap_legend = TRUE, |
38 |
+ heatmap_legend_title = name, |
|
38 | 39 |
heatmap_legend_color_bar = c("discrete", "continuous"))} |
39 | 40 |
\arguments{ |
40 | 41 |
|
... | ... |
@@ -86,6 +87,7 @@ Heatmap(matrix, col, name, na_col = "grey", rect_gp = gpar(col = NA), |
86 | 87 |
\item{combined_name_fun}{if the heatmap is split by rows, how to make a combined row title for each slice?The input parameter for this function is a vector which contains level names under each column in \code{split}.} |
87 | 88 |
\item{width}{the width of the single heatmap, should be a fixed \code{\link[grid]{unit}} object. It is used for the layout when the heatmapis appended to a list of heatmaps.} |
88 | 89 |
\item{show_heatmap_legend}{whether show heatmap legend?} |
90 |
+ \item{heatmap_legend_title}{title for the heatmap legend. By default it is the name of the heatmap} |
|
89 | 91 |
\item{heatmap_legend_color_bar}{if the matrix is continuous, whether should the legend as continuous color bar as well?} |
90 | 92 |
} |
91 | 93 |
\details{ |
... | ... |
@@ -113,8 +115,6 @@ Zuguang Gu <z.gu@dkfz.de> |
113 | 115 |
|
114 | 116 |
} |
115 | 117 |
\examples{ |
116 |
- |
|
117 |
- |
|
118 | 118 |
mat = matrix(rnorm(80, 2), 8, 10) |
119 | 119 |
mat = rbind(mat, matrix(rnorm(40, -2), 4, 10)) |
120 | 120 |
rownames(mat) = letters[1:12] |
... | ... |
@@ -20,8 +20,6 @@ This function is only for internal use. |
20 | 20 |
|
21 | 21 |
} |
22 | 22 |
\section{Detials}{ |
23 |
- |
|
24 |
- |
|
25 | 23 |
This function is only for internal use.} |
26 | 24 |
\value{ |
27 | 25 |
A \code{\link[grid]{unit}} object. |
... | ... |
@@ -32,6 +30,4 @@ Zuguang Gu <z.gu@dkfz.de> |
32 | 30 |
|
33 | 31 |
} |
34 | 32 |
\examples{ |
35 |
- |
|
36 |
- |
|
37 | 33 |
# no example for this internal method} |
... | ... |
@@ -690,7 +690,7 @@ draw(ht1 + ht2) |
690 | 690 |
``` |
691 | 691 |
|
692 | 692 |
**ComplexHeatmap** only generates legends for heatmaps and simple annotations. Self-defined legends |
693 |
-can be passed by 1heatmap_legend_list` and `annotation_legend_list` as a list of `grob` objects. |
|
693 |
+can be passed by `heatmap_legend_list` and `annotation_legend_list` as a list of `grob` objects. |
|
694 | 694 |
|
695 | 695 |
```{r self_defined_annotation_legend, fig.width = 10} |
696 | 696 |
ha = HeatmapAnnotation(points = anno_points(rnorm(10))) |
... | ... |
@@ -699,6 +699,13 @@ lgd = legendGrob(c("dots"), pch = 16) |
699 | 699 |
draw(ht1 + ht2, annotation_legend_list = list(lgd)) |
700 | 700 |
``` |
701 | 701 |
|
702 |
+Finally, you can change legend title for heatmap legend by specifying `heatmap_legend_title` in `Heatmap()`: |
|
703 |
+ |
|
704 |
+```{r} |
|
705 |
+ht1 = Heatmap(mat, name = "ht1", column_title = "Heatmap 1", heatmap_legend_title = expression(alpha)) |
|
706 |
+ht2 = Heatmap(mat, name = "ht2", column_title = "Heatmap 2", heatmap_legend_title = "HHHHH") |
|
707 |
+draw(ht1 + ht2) |
|
708 |
+``` |
|
702 | 709 |
|
703 | 710 |
### Gaps between heatmaps |
704 | 711 |
|