Browse code

Commit made by the Bioconductor Git-SVN bridge.

Commit id: 412fce42e41a87c7be53141c33f5d08beedbb27a

optimized axis in annotations



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

z.gu authored on 01/08/2015 17:25:23
Showing 5 changed files

... ...
@@ -83,6 +83,7 @@ HeatmapAnnotation = function(df, name, col, color_bar = rep("discrete", ncol(df)
83 83
 	}
84 84
 
85 85
 	.Object@name = name
86
+	n_anno = 0
86 87
 
87 88
 	if(!missing(df)) {
88 89
 		if(is.null(colnames(df))) {
... ...
@@ -259,6 +260,7 @@ setMethod(f = "get_color_mapping_list",
259 260
 # -object a `HeatmapAnnotation-class` object.
260 261
 # -index a vector of order.
261 262
 # -k if row annotation is splitted, the value identifies which row slice.
263
+# -n total number of row slices.
262 264
 # -... pass to `grid::viewport` which contains all annotations.
263 265
 #
264 266
 # == details
... ...
@@ -272,7 +274,7 @@ setMethod(f = "get_color_mapping_list",
272 274
 #
273 275
 setMethod(f = "draw",
274 276
 	signature = "HeatmapAnnotation",
275
-	definition = function(object, index, k = NULL, ...) {
277
+	definition = function(object, index, k = NULL, n = NULL, ...) {
276 278
 
277 279
 	which = object@which
278 280
 	n_anno = length(object@anno_list)
... ...
@@ -286,7 +288,7 @@ setMethod(f = "draw",
286 288
 		} else {
287 289
 			pushViewport(viewport(x = sum(anno_size[seq_len(i)]) + sum(gap[seq_len(i)]) - gap[i], width = anno_size[i], just = c("right", "center")))
288 290
 		}
289
-		draw(object@anno_list[[i]], index, k)
291
+		draw(object@anno_list[[i]], index, k, n)
290 292
 		upViewport()
291 293
 	}
292 294
 	upViewport()
... ...
@@ -863,7 +863,7 @@ setMethod(f = "draw_heatmap_list",
863 863
         } else if(inherits(ht, "HeatmapAnnotation")) {
864 864
             # calcualte the position of the heatmap body
865 865
             for(j in seq_len(n_slice)) {
866
-                draw(ht, index = htkk@row_order_list[[j]], y = slice_y[j], height = slice_height[j], just = c("center", "top"), k = j)
866
+                draw(ht, index = htkk@row_order_list[[j]], y = slice_y[j], height = slice_height[j], just = c("center", "top"), k = j, n = n_slice)
867 867
             }
868 868
         }
869 869
         upViewport()
... ...
@@ -37,7 +37,7 @@ SingleAnnotation = setClass("SingleAnnotation",
37 37
 	prototype = list(
38 38
 		color_mapping = NULL,
39 39
 		fun = function(index) NULL,
40
-		show = TRUE
40
+		show_legend= TRUE
41 41
 	)
42 42
 )
43 43
 
... ...
@@ -131,7 +131,7 @@ SingleAnnotation = function(name, value, col, fun, which = c("column", "row"),
131 131
 				grid.rect(x[l], y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill[l]), gp)))
132 132
 			}
133 133
 		} else {
134
-			.Object@fun = function(index, k = NULL) {
134
+			.Object@fun = function(index, k = NULL, N = NULL) {
135 135
 				n = length(index)
136 136
 				y = (n - seq_len(n) + 0.5) / n
137 137
 				fill = map_to_colors(color_mapping, value[index])
... ...
@@ -148,7 +148,7 @@ SingleAnnotation = function(name, value, col, fun, which = c("column", "row"),
148 148
 
149 149
     if(which == "row") {
150 150
     	if(length(formals(.Object@fun)) == 1) {
151
-    		formals(.Object@fun) = alist(index = , k = NULL)
151
+    		formals(.Object@fun) = alist(index = , k = NULL, N = NULL)
152 152
     	}
153 153
     }
154 154
 
... ...
@@ -163,6 +163,7 @@ SingleAnnotation = function(name, value, col, fun, which = c("column", "row"),
163 163
 # -index a vector of orders
164 164
 # -k if row annotation is splitted, the value identifies which row slice. It is only used for the naems of the viewport
165 165
 #    which contains the annotation graphics.
166
+# -n total number of row slices
166 167
 #
167 168
 # == details
168 169
 # A viewport is created.
... ...
@@ -177,14 +178,14 @@ SingleAnnotation = function(name, value, col, fun, which = c("column", "row"),
177 178
 #
178 179
 setMethod(f = "draw",
179 180
 	signature = "SingleAnnotation",
180
-	definition = function(object, index, k = NULL) {
181
+	definition = function(object, index, k = NULL, n = NULL) {
181 182
 
182 183
 	if(is.null(k)) {
183 184
 		pushViewport(viewport(name = paste("annotation", object@name, sep = "_")))
184 185
 		object@fun(index)
185 186
 	} else {
186 187
 		pushViewport(viewport(name = paste("annotation", object@name, k, sep = "_")))
187
-		object@fun(index, k)
188
+		object@fun(index, k, n)
188 189
 	}
189 190
 	upViewport()
190 191
 
... ...
@@ -44,7 +44,7 @@ anno_points = function(x, which = c("column", "row"), gp = gpar(), pch = 16,
44 44
 	}
45 45
 
46 46
 	switch(which,
47
-		row = function(index, k = NULL) {
47
+		row = function(index, k = NULL, N = NULL) {
48 48
 			n = length(index)
49 49
 			
50 50
 			if(is.null(k)) {
... ...
@@ -57,9 +57,9 @@ anno_points = function(x, which = c("column", "row"), gp = gpar(), pch = 16,
57 57
 			grid.rect()
58 58
 			grid.points(x[index], n - seq_along(index) + 1, gp = gp, default.units = "native", pch = pch, size = size)
59 59
 			if(axis) {
60
-				if(axis_side == "top") {
60
+				if(k == 1 && axis_side == "top") {
61 61
 					grid.xaxis(main = FALSE, gp = axis_gp)
62
-				} else {
62
+				} else if(k == N && axis_side == "bottom") {
63 63
 					grid.xaxis(gp = axis_gp)
64 64
 				}
65 65
 			}
... ...
@@ -128,7 +128,7 @@ anno_barplot = function(x, which = c("column", "row"),
128 128
 	}
129 129
 
130 130
 	switch(which,
131
-		row = function(index, k = NULL) {
131
+		row = function(index, k = NULL, N = NULL) {
132 132
 			n = length(index)
133 133
 			
134 134
 			if(is.null(k)) {
... ...
@@ -141,9 +141,9 @@ anno_barplot = function(x, which = c("column", "row"),
141 141
 			grid.rect()
142 142
 			grid.rect(x = data_scale[1], y = n - seq_along(index) + 1, width = x[index] - data_scale[1], height = 1*factor, just = "left", default.units = "native", gp = gp)
143 143
 			if(axis) {
144
-				if(axis_side == "top") {
144
+				if(k == 1 && axis_side == "top") {
145 145
 					grid.xaxis(main = FALSE, gp = axis_gp)
146
-				} else {
146
+				} else if(k == N && axis_side == "bottom") {
147 147
 					grid.xaxis(gp = axis_gp)
148 148
 				}
149 149
 			}
... ...
@@ -215,7 +215,7 @@ anno_boxplot = function(x, which = c("column", "row"), gp = gpar(fill = "#CCCCCC
215 215
 	}
216 216
 
217 217
 	switch(which,
218
-		row = function(index, k = NULL) {
218
+		row = function(index, k = NULL, N = NULL) {
219 219
 			if(is.matrix(x)) {
220 220
 				x = x[index, , drop = FALSE]
221 221
 				boxplot_stats = boxplot(t(x), plot = FALSE)$stats
... ...
@@ -248,9 +248,9 @@ anno_boxplot = function(x, which = c("column", "row"), gp = gpar(fill = "#CCCCCC
248 248
 				default.units = "native", gp = gp)
249 249
 			grid.points(x = boxplot_stats[3, ], y = n - seq_along(index) + 1, default.units = "native", gp = gp, pch = pch, size = size)
250 250
 			if(axis) {
251
-				if(axis_side == "top") {
251
+				if(k == 1 && axis_side == "top") {
252 252
 					grid.xaxis(main = FALSE, gp = axis_gp)
253
-				} else {
253
+				} else if(k == N && axis_side == "bottom") {
254 254
 					grid.xaxis(gp = axis_gp)
255 255
 				}
256 256
 			}
... ...
@@ -318,7 +318,7 @@ anno_histogram = function(x, which = c("column", "row"), gp = gpar(fill = "#CCCC
318 318
 	gp = check_gp(gp)
319 319
 
320 320
 	switch(which,
321
-		row = function(index, k = NULL) {
321
+		row = function(index, k = NULL, N = NULL) {
322 322
 			if(is.matrix(x)) {
323 323
 				x = x[index, , drop = FALSE]
324 324
 				x_range =range(x)
... ...
@@ -414,7 +414,7 @@ anno_density = function(x, which = c("column", "row"), gp = gpar(fill = "#CCCCCC
414 414
 	gp = check_gp(gp)
415 415
 
416 416
 	switch(which,
417
-		row = function(index, k = NULL) {
417
+		row = function(index, k = NULL, N = NULL) {
418 418
 			if(is.matrix(x)) {
419 419
 				x = x[index, , drop = FALSE]
420 420
 				density_stats = apply(x, 1, density, ...)
... ...
@@ -548,7 +548,7 @@ anno_text = function(x, which = c("column", "row"), gp = gpar(), rot = 0,
548 548
 	gp = check_gp(gp)
549 549
 
550 550
 	switch(which,
551
-		row = function(index, k = NULL) {
551
+		row = function(index, k = NULL, N = NULL) {
552 552
 			n = length(index)
553 553
 			
554 554
 			if(is.null(k)) {
... ...
@@ -845,7 +845,7 @@ splitted as well.
845 845
 
846 846
 ```{r heatmap_list_with_row_annotation_complex}
847 847
 ht1 = Heatmap(mat, name = "ht1", km = 2)
848
-ha_boxplot = rowAnnotation(boxplot = anno_boxplot(mat, which = "row"), 
848
+ha_boxplot = rowAnnotation(boxplot = anno_boxplot(mat, axis = TRUE, which = "row"), 
849 849
     width = unit(2, "cm"))
850 850
 ha_boxplot + ht1
851 851
 ```
... ...
@@ -854,7 +854,7 @@ When row split is applied, graphical parameters for annotation function can be s
854 854
 as the number of row slices.
855 855
 
856 856
 ```{r heatmap_list_with_row_annotation_graphical_parameter}
857
-ha_boxplot2 = rowAnnotation(boxplot = anno_boxplot(mat, gp = gpar(fill = c("red", "blue")), which = "row"), 
857
+ha_boxplot2 = rowAnnotation(boxplot = anno_boxplot(mat, gp = gpar(fill = c("red", "blue")), axis = TRUE, which = "row"), 
858 858
     width = unit(2, "cm"))
859 859
 ha_boxplot2 + ht1
860 860
 ```