Browse code

anno_block(): add labels_offset and labels_just argument

Zuguang Gu authored on 22/01/2021 09:39:05
Showing6 changed files

... ...
@@ -1,8 +1,8 @@
1 1
 Package: ComplexHeatmap
2 2
 Type: Package
3 3
 Title: Make Complex Heatmaps
4
-Version: 2.7.4
5
-Date: 2020-12-31
4
+Version: 2.7.5.1000
5
+Date: 2021-1-22
6 6
 Author: Zuguang Gu
7 7
 Maintainer: Zuguang Gu <z.gu@dkfz.de>
8 8
 Depends: R (>= 3.1.2), methods, grid, graphics, stats, grDevices
... ...
@@ -1,3 +1,10 @@
1
+CHANGES in VERSION 2.7.5
2
+
3
+* `anno_block`: add `labels_offset` and `labels_just`.
4
+* `anno_lines`: `show_points` can be a vector.
5
+
6
+==========================
7
+
1 8
 CHANGES in VERSION 2.7.4
2 9
 
3 10
 * add `save_last` option in `ht_opt()`.
... ...
@@ -976,9 +976,16 @@ anno_lines = function(x, which = c("column", "row"), border = TRUE, gp = gpar(),
976 976
 				} else {
977 977
 					grid.lines(y, x, gp = subset_gp(gp, i), default.units = "native")
978 978
 				}
979
-				if(add_points) {
980
-					grid.points(y, x, gp = subset_gp(pt_gp, i), 
981
-						default.units = "native", pch = pch[i], size = size[i])
979
+				if(length(add_points) == ncol(value)) {
980
+					if(add_points[i]) {
981
+						grid.points(y, x, gp = subset_gp(pt_gp, i), 
982
+							default.units = "native", pch = pch[i], size = size[i])
983
+					}
984
+				} else {
985
+					if(add_points) {
986
+						grid.points(y, x, gp = subset_gp(pt_gp, i), 
987
+							default.units = "native", pch = pch[i], size = size[i])
988
+					}
982 989
 				}
983 990
 			}
984 991
 		} else {
... ...
@@ -1027,9 +1034,16 @@ anno_lines = function(x, which = c("column", "row"), border = TRUE, gp = gpar(),
1027 1034
 				} else {
1028 1035
 					grid.lines(x, y, gp = subset_gp(gp, i), default.units = "native")
1029 1036
 				}
1030
-				if(add_points) {
1031
-					grid.points(x, y, gp = subset_gp(pt_gp, i), 
1032
-						default.units = "native", pch = pch[i], size = size[i])
1037
+				if(length(add_points) == ncol(value)) {
1038
+					if(add_points[i]) {
1039
+						grid.points(x, y, gp = subset_gp(pt_gp, i), 
1040
+							default.units = "native", pch = pch[i], size = size[i])
1041
+					}
1042
+				} else {
1043
+					if(add_points) {
1044
+						grid.points(x, y, gp = subset_gp(pt_gp, i), 
1045
+							default.units = "native", pch = pch[i], size = size[i])
1046
+					}
1033 1047
 				}
1034 1048
 			}
1035 1049
 		} else {
... ...
@@ -1808,11 +1822,13 @@ anno_density = function(x, which = c("column", "row"),
1808 1822
 			density_y[[i]] = c(0, density_y[[i]], 0)
1809 1823
 		}
1810 1824
 	}
1811
-	xscale = xscale + c(-0.025, 0.025)*(xscale[2] - xscale[1])
1825
+	
1812 1826
 	if(type == "lines") {
1827
+		xscale = xscale + c(-0.025, 0.025)*(xscale[2] - xscale[1])
1813 1828
 		yscale = c(0, max(unlist(density_y)))
1814 1829
 		yscale[2] = yscale[2]*1.05
1815 1830
 	} else if(type == "violin") {
1831
+		xscale = xscale + c(-0.025, 0.025)*(xscale[2] - xscale[1])
1816 1832
 		yscale = max(unlist(density_y))
1817 1833
 		yscale = c(-yscale*1.05, yscale*1.05)
1818 1834
 	} else if(type == "heatmap") {
... ...
@@ -3249,6 +3265,8 @@ anno_summary = function(which = c("column", "row"), border = TRUE, bar_width = 0
3249 3265
 # -labels Labels put on blocks.
3250 3266
 # -labels_gp Graphic parameters for labels.
3251 3267
 # -labels_rot Rotation for labels.
3268
+# -labels_offset Positions of the labels. It controls offset on y-directions for column annotation and on x-directoin for row annotation.
3269
+# -labels_just Jusification of the labels.
3252 3270
 # -which Is it a row annotation or a column annotation?
3253 3271
 # -width Width of the annotation. The value should be an absolute unit. Width is not allowed to be set for column annotation.
3254 3272
 # -height Height of the annotation. The value should be an absolute unit. Height is not allowed to be set for row annotation.
... ...
@@ -3271,7 +3289,9 @@ anno_summary = function(which = c("column", "row"), border = TRUE, bar_width = 0
3271 3289
 #     left_annotation = rowAnnotation(foo = anno_block(gp = gpar(fill = 2:4),
3272 3290
 #         labels = c("group1", "group2", "group3"), labels_gp = gpar(col = "white"))),
3273 3291
 #     row_km = 3)
3274
-anno_block = function(gp = gpar(), labels = NULL, labels_gp = gpar(), labels_rot = ifelse(which == "row", 90, 0),
3292
+anno_block = function(gp = gpar(), labels = NULL, labels_gp = gpar(), 
3293
+	labels_rot = ifelse(which == "row", 90, 0),
3294
+	labels_offset = unit(0.5, "npc"), labels_just = "center",
3275 3295
 	which = c("column", "row"), width = NULL, height = NULL, show_name = FALSE) {
3276 3296
 
3277 3297
 	if(is.null(.ENV$current_annotation_which)) {
... ...
@@ -3313,7 +3333,10 @@ anno_block = function(gp = gpar(), labels = NULL, labels_gp = gpar(), labels_rot
3313 3333
 			}
3314 3334
 			label = labels[k]
3315 3335
 			labels_gp = subset_gp(recycle_gp(labels_gp, n), k)
3316
-			grid.text(label, gp = labels_gp, rot = labels_rot)
3336
+			x = y = unit(0.5, "npc")
3337
+			if(which == "column") y = labels_offset
3338
+			if(which == "row") x = labels_offset
3339
+			grid.text(label, x = x, y = y, gp = labels_gp, rot = labels_rot, just = labels_just)
3317 3340
 		}
3318 3341
 	}
3319 3342
 
... ...
@@ -3322,7 +3345,7 @@ anno_block = function(gp = gpar(), labels = NULL, labels_gp = gpar(), labels_rot
3322 3345
 		n = NA,
3323 3346
 		fun_name = "anno_block",
3324 3347
 		which = which,
3325
-		var_import = list(gp, labels, labels_gp, labels_rot),
3348
+		var_import = list(gp, labels, labels_gp, labels_rot, labels_offset, labels_just, which),
3326 3349
 		subset_rule = list(),
3327 3350
 		subsetable = TRUE,
3328 3351
 		height = anno_size$height,
... ...
@@ -203,7 +203,6 @@ ht_opt = setGlobalOptions(
203 203
 		.value = FALSE
204 204
 	),
205 205
 	save_last = list(
206
-		.visible = FALSE,
207 206
 		.value = FALSE
208 207
 	)
209 208
 )
... ...
@@ -7,7 +7,9 @@ Block annotation
7 7
 Block annotation
8 8
 }
9 9
 \usage{
10
-anno_block(gp = gpar(), labels = NULL, labels_gp = gpar(), labels_rot = ifelse(which == "row", 90, 0),
10
+anno_block(gp = gpar(), labels = NULL, labels_gp = gpar(),
11
+    labels_rot = ifelse(which == "row", 90, 0),
12
+    labels_offset = unit(0.5, "npc"), labels_just = "center",
11 13
     which = c("column", "row"), width = NULL, height = NULL, show_name = FALSE)
12 14
 }
13 15
 \arguments{
... ...
@@ -16,6 +18,8 @@ anno_block(gp = gpar(), labels = NULL, labels_gp = gpar(), labels_rot = ifelse(w
16 18
   \item{labels}{Labels put on blocks.}
17 19
   \item{labels_gp}{Graphic parameters for labels.}
18 20
   \item{labels_rot}{Rotation for labels.}
21
+  \item{labels_offset}{Positions of the labels. It controls offset on y-directions for column annotation and on x-directoin for row annotation.}
22
+  \item{labels_just}{Jusification of the labels.}
19 23
   \item{which}{Is it a row annotation or a column annotation?}
20 24
   \item{width}{Width of the annotation. The value should be an absolute unit. Width is not allowed to be set for column annotation.}
21 25
   \item{height}{Height of the annotation. The value should be an absolute unit. Height is not allowed to be set for row annotation.}
... ...
@@ -59,7 +59,7 @@ for(i in 1:10) {
59 59
     bed = bed[sample(nrow(bed), 20), , drop = FALSE]
60 60
     gr_cnv = GRanges(seqnames = bed[, 1], ranges = IRanges(bed[, 2], bed[, 3]))
61 61
 
62
-char_mat = cbind(char_mat, normalize_genomic_signals_to_bins(gr_cnv, bed[, 4]))
62
+    char_mat = cbind(char_mat, normalize_genomic_signals_to_bins(gr_cnv, bed[, 4]))
63 63
 }
64 64
 
65 65
 #### two numeric columns ##########