Browse code

fix gp bug

Zuguang Gu authored on 19/10/2018 11:53:11
Showing2 changed files

... ...
@@ -215,13 +215,16 @@ anno_simple = function(x, col, na_col = "grey",
215 215
                 if(!is.null(pch)) {
216 216
 					l = !is.na(pch[, i])
217 217
 					grid.points(x = rep((i-0.5)/nc, sum(l)), y = y[l], pch = pch[l, i], 
218
-						size = pt_size, gp = pt_gp)
218
+						size = pt_size[i], gp = subset_gp(pt_gp, i))
219 219
 				}
220 220
             }
221 221
         } else {
222 222
 			fill = map_to_colors(color_mapping, value[index])
223 223
 			grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
224 224
 			if(!is.null(pch)) {
225
+				pch = pch[index]
226
+				pt_size = pt_size[index]
227
+				pt_gp = subset_gp(pt_gp, index)
225 228
 				l = !is.na(pch)
226 229
 				grid.points(x = rep(0.5, sum(l)), y = y[l], pch = pch[l], size = pt_size[l], 
227 230
 					gp = subset_gp(pt_gp, which(l)))
... ...
@@ -242,7 +245,8 @@ anno_simple = function(x, col, na_col = "grey",
242 245
                 if(!is.null(pch)) {
243 246
                 	pch = pch[index, , drop = FALSE]
244 247
 					l = !is.na(pch[, i])
245
-					grid.points(x[l], y = rep((nc-i +0.5)/nc, sum(l)), pch = pch[l, i], size = pt_size, gp = pt_gp)
248
+					grid.points(x[l], y = rep((nc-i +0.5)/nc, sum(l)), pch = pch[l, i], 
249
+						size = pt_size[i], gp = subset_gp(pt_gp, i))
246 250
 				}
247 251
             }
248 252
         } else {
... ...
@@ -250,8 +254,11 @@ anno_simple = function(x, col, na_col = "grey",
250 254
 			grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
251 255
 			if(!is.null(pch)) {
252 256
 				pch = pch[index]
257
+				pt_size = pt_size[index]
258
+				pt_gp = subset_gp(pt_gp, index)
253 259
 				l = !is.na(pch)
254
-				grid.points(x[l], y = rep(0.5, sum(l)), pch = pch[l], size = pt_size[l], gp = subset_gp(pt_gp, which(l)))
260
+				grid.points(x[l], y = rep(0.5, sum(l)), pch = pch[l], size = pt_size[l], 
261
+					gp = subset_gp(pt_gp, which(l)))
255 262
 			}
256 263
         }
257 264
         if(border) grid.rect(gp = gpar(fill = "transparent"))
... ...
@@ -644,6 +651,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
644 651
 	axis_grob = if(axis) construct_axis_grob(axis_param, which, data_scale) else NULL
645 652
 
646 653
 	row_fun = function(index, k = 1, N = 1) {
654
+		
647 655
 		n = length(index)
648 656
 
649 657
 		pushViewport(viewport(xscale = data_scale, yscale = c(0.5, n+0.5)))
... ...
@@ -653,7 +661,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
653 661
 					default.units = "native", pch = pch[i], size = size[i])
654 662
 			}
655 663
 		} else {
656
-			grid.points(value[index], n - seq_along(index) + 1, gp = gp, default.units = "native", 
664
+			grid.points(value[index], n - seq_along(index) + 1, gp = subset_gp(gp, index), default.units = "native", 
657 665
 				pch = pch[index], size = size[index])
658 666
 		}
659 667
 		if(axis_param$side == "top") {
... ...
@@ -667,6 +675,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
667 675
 	}
668 676
 
669 677
 	column_fun = function(index, k = 1, N = 1) {
678
+		
670 679
 		n = length(index)
671 680
 		
672 681
 		pushViewport(viewport(yscale = data_scale, xscale = c(0.5, n+0.5)))
... ...
@@ -675,7 +684,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
675 684
 				grid.points(seq_along(index), value[index, i], gp = subset_gp(gp, i), default.units = "native", pch = pch[i], size = size[i])
676 685
 			}
677 686
 		} else {
678
-			grid.points(seq_along(index), value[index], gp = gp, default.units = "native", pch = pch[index], size = size[index])
687
+			grid.points(seq_along(index), value[index], gp = subset_gp(gp, index), default.units = "native", pch = pch[index], size = size[index])
679 688
 		}
680 689
 		if(axis_param$side == "left") {
681 690
 			if(k > 1) axis = FALSE
... ...
@@ -857,7 +866,7 @@ anno_lines = function(x, which = c("column", "row"), border = TRUE, gp = gpar(),
857 866
 				grid.lines(y, x, gp = gp, default.units = "native")
858 867
 			}
859 868
 			if(add_points) {
860
-				grid.points(y, x, gp = gp, default.units = "native", 
869
+				grid.points(y, x, gp = subset_gp(pt_gp, index), default.units = "native", 
861 870
 					pch = pch[index], size = size[index])
862 871
 			}
863 872
 		}
... ...
@@ -904,7 +913,7 @@ anno_lines = function(x, which = c("column", "row"), border = TRUE, gp = gpar(),
904 913
 				grid.lines(x, y, gp = gp, default.units = "native")
905 914
 			}
906 915
 			if(add_points) {
907
-				grid.points(seq_along(index), value[index], gp = pt_gp, default.units = "native", 
916
+				grid.points(seq_along(index), value[index], gp = subset_gp(pt_gp, index), default.units = "native", 
908 917
 					pch = pch[index], size = size[index])
909 918
 			}
910 919
 		}
... ...
@@ -1207,6 +1216,8 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,
1207 1216
 		
1208 1217
 		n = length(index)
1209 1218
 		gp = subset_gp(gp, index)
1219
+		pch = pch[index]
1220
+		size = size[index]
1210 1221
 		pushViewport(viewport(xscale = data_scale, yscale = c(0.5, n+0.5)))
1211 1222
 		
1212 1223
 		grid.rect(x = boxplot_stats[2, ], y = n - seq_along(index) + 1,  
... ...
@@ -1253,6 +1264,8 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,
1253 1264
 
1254 1265
 		n = length(index)
1255 1266
 		gp = subset_gp(gp, index)
1267
+		pch = pch[index]
1268
+		size = size[index]
1256 1269
 		pushViewport(viewport(xscale = c(0.5, n+0.5), yscale = data_scale))
1257 1270
 		grid.rect(x = seq_along(index), y = boxplot_stats[2, ], 
1258 1271
 			height = boxplot_stats[4, ] - boxplot_stats[2, ], width = 1*box_width, just = "bottom", 
... ...
@@ -1432,7 +1445,7 @@ anno_histogram = function(x, which = c("column", "row"), n_breaks = 11,
1432 1445
 			pushViewport(viewport(y = unit(0, "npc"), x = unit(i/n, "npc"), width = unit(1/n, "npc"), 
1433 1446
 				just = c("right", "bottom"), xscale = xscale, yscale = yscale))
1434 1447
 			grid.rect(y = histogram_breaks[[i]][-1], x = 0, height = histogram_breaks[[i]][-1] - histogram_breaks[[i]][-n_breaks], 
1435
-				width = histogram_counts[[i]], just = c("left", "top"), default.units = "native", gp = subset_gp(gp, index[i]))	
1448
+				width = histogram_counts[[i]], just = c("left", "top"), default.units = "native", gp = subset_gp(gp, i))	
1436 1449
 			popViewport()
1437 1450
 		}
1438 1451
 		pushViewport(viewport(yscale = yscale))
... ...
@@ -1658,11 +1671,11 @@ anno_density = function(x, which = c("column", "row"),
1658 1671
 				just = c("right", "bottom"), xscale = xscale, yscale = yscale))
1659 1672
 			if(type == "lines") {
1660 1673
 				grid.polygon(y = density_x[[i]], x = density_y[[i]]*joyplot_scale, 
1661
-					default.units = "native", gp = subset_gp(gp, index[i]))
1674
+					default.units = "native", gp = subset_gp(gp, i))
1662 1675
 			} else if(type == "violin") {
1663 1676
 				grid.polygon(y = c(density_x[[i]], rev(density_x[[i]])), 
1664 1677
 					x = c(density_y[[i]], -rev(density_y[[i]])), default.units = "native", 
1665
-					gp = subset_gp(gp, index[i]))
1678
+					gp = subset_gp(gp, i))
1666 1679
 				box_stat = boxplot(value[[i]], plot = FALSE)$stat
1667 1680
 				grid.lines(y = box_stat[1:2, 1], x = c(0, 0), default.units = "native", 
1668 1681
 					gp = subset_gp(gp, i))
... ...
@@ -2007,7 +2020,7 @@ anno_joyplot = function(x, which = c("column", "row"), gp = gpar(fill = "#000000
2007 2020
 		
2008 2021
 		n = length(index)
2009 2022
 		
2010
-		gp = subset(gp, index)
2023
+		gp = subset_gp(gp, index)
2011 2024
 
2012 2025
 		for(i in seq_len(n)) {
2013 2026
 			pushViewport(viewport(y = unit(0, "npc"), x = unit(i/n, "npc"), 
... ...
@@ -2155,7 +2168,7 @@ anno_horizon = function(x, which = c("column", "row"),
2155 2168
 	}
2156 2169
 
2157 2170
 	if(which == "column") {
2158
-		stop_wrap("anno_horizon() does not support column annotation. If you want, please email me.")
2171
+		stop_wrap("anno_horizon() does not support column annotation.")
2159 2172
 	}
2160 2173
 
2161 2174
 	if(normalize) {
... ...
@@ -115,6 +115,10 @@ draw(anno, test = "anno_points")
115 115
 anno = anno_points(cbind(c(1:5, 1:5), c(5:1, 5:1)), gp = gpar(col = 2:3))
116 116
 draw(anno, test = "matrix")
117 117
 
118
+anno = anno_points(1:10, gp = gpar(col = rep(2:3, each = 5)), pch = rep(2:3, each = 5))
119
+draw(anno, test = "anno_points")
120
+draw(anno, index = c(1, 3, 5, 7, 9, 2, 4, 6, 8, 10), test = "anno_points")
121
+
118 122
 ##### test anno_lines ###
119 123
 anno = anno_lines(runif(10))
120 124
 draw(anno, test = "anno_lines")
... ...
@@ -260,3 +264,85 @@ Heatmap(m) + rowAnnotation(mark = anno)
260 264
 
261 265
 ht_list = Heatmap(m, cluster_rows = F, cluster_columns = F) + rowAnnotation(mark = anno)
262 266
 draw(ht_list, row_split = c(rep("a", 95), rep("b", 5)))
267
+
268
+
269
+### graphic parameters after reordering
270
+index = c(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
271
+anno = anno_simple(1:10, pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
272
+	pt_size = unit(1:10, "mm"))
273
+draw(anno, index, test = "a numeric vector")
274
+anno = anno_simple(1:10, pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
275
+	pt_size = unit(1:10, "mm"), which = "row")
276
+draw(anno, index, test = "a numeric vector")
277
+
278
+
279
+anno = anno_points(1:10, pch = 1:10, gp = gpar(col = rep(c(1, 2), each = 5)),
280
+	size = unit(1:10, "mm"))
281
+draw(anno, index, test = "a numeric vector")
282
+anno = anno_points(1:10, pch = 1:10, gp = gpar(col = rep(c(1, 2), each = 5)),
283
+	size = unit(1:10, "mm"), which = "row")
284
+draw(anno, index, test = "a numeric vector")
285
+
286
+
287
+anno = anno_lines(sort(runif(10)), pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
288
+	size = unit(1:10, "mm"), add_points = TRUE)
289
+draw(anno, index, test = "a numeric vector")
290
+anno = anno_lines(sort(runif(10)), pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
291
+	size = unit(1:10, "mm"), add_points = TRUE, which = "row")
292
+draw(anno, index, test = "a numeric vector")
293
+
294
+
295
+anno = anno_barplot(1:10, gp = gpar(fill = rep(c(1, 2), each = 5)))
296
+draw(anno, index, test = "a numeric vector")
297
+anno = anno_barplot(1:10, gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
298
+draw(anno, index, test = "a numeric vector")
299
+
300
+anno = anno_barplot(cbind(1:10, 10:1), gp = gpar(fill = 1:2))
301
+draw(anno, index, test = "a numeric vector")
302
+anno = anno_barplot(cbind(1:10, 10:1), gp = gpar(fill = 1:2), which = "row")
303
+draw(anno, index, test = "a numeric vector")
304
+
305
+
306
+m = matrix(rnorm(100), 10)
307
+m = m[, order(apply(m, 2, median))]
308
+anno = anno_boxplot(m, pch = 1:10, gp = gpar(fill = rep(c(1, 2), each = 5)),
309
+	size = unit(1:10, "mm"), height = unit(4, "cm"))
310
+draw(anno, index, test = "a numeric vector")
311
+anno = anno_boxplot(t(m), pch = 1:10, gp = gpar(fill = rep(c(1, 2), each = 5)),
312
+	size = unit(1:10, "mm"), which = "row", width = unit(4, "cm"))
313
+draw(anno, index, test = "a numeric vector")
314
+
315
+anno = anno_histogram(m, gp = gpar(fill = rep(c(1, 2), each = 5)))
316
+draw(anno, index, test = "a numeric vector")
317
+anno = anno_histogram(t(m), gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
318
+draw(anno, index, test = "a numeric vector")
319
+
320
+anno = anno_density(m, gp = gpar(fill = rep(c(1, 2), each = 5)))
321
+draw(anno, index, test = "a numeric vector")
322
+anno = anno_density(t(m), gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
323
+draw(anno, index, test = "a numeric vector")
324
+
325
+
326
+anno = anno_density(m, type = "violin", gp = gpar(fill = rep(c(1, 2), each = 5)))
327
+draw(anno, index, test = "a numeric vector")
328
+anno = anno_density(t(m), type = "violin", gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
329
+draw(anno, index, test = "a numeric vector")
330
+
331
+
332
+anno = anno_text(month.name, gp = gpar(col = rep(c(1, 2), each = 5)))
333
+draw(anno, index, test = "a numeric vector")
334
+anno = anno_text(month.name, gp = gpar(col = rep(c(1, 2), each = 5)), which= "row")
335
+draw(anno, index, test = "a numeric vector")
336
+
337
+lt = lapply(1:10, function(x) cumprod(1 + runif(1000, -x/100, x/100)) - 1)
338
+anno = anno_horizon(lt, gp = gpar(pos_fill = rep(c(1, 2), each = 5), neg_fill = rep(c(3, 4), each = 5)), which = "row")
339
+draw(anno, index, test = "a numeric vector")
340
+
341
+m = matrix(rnorm(1000), nc = 10)
342
+lt = apply(m, 2, function(x) data.frame(density(x)[c("x", "y")]))
343
+anno = anno_joyplot(lt, gp = gpar(fill = rep(c(1, 2), each = 5)), 
344
+	width = unit(4, "cm"), which = "row")
345
+draw(anno, index, test = "joyplot")
346
+
347
+
348
+