Skip to content

Commit

Permalink
fix gp bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jokergoo committed Oct 19, 2018
1 parent 7db3856 commit c3ef40c
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 12 deletions.
37 changes: 25 additions & 12 deletions R/AnnotationFunction-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,16 @@ anno_simple = function(x, col, na_col = "grey",
if(!is.null(pch)) {
l = !is.na(pch[, i])
grid.points(x = rep((i-0.5)/nc, sum(l)), y = y[l], pch = pch[l, i],
size = pt_size, gp = pt_gp)
size = pt_size[i], gp = subset_gp(pt_gp, i))
}
}
} else {
fill = map_to_colors(color_mapping, value[index])
grid.rect(x = 0.5, y, height = 1/n, width = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
if(!is.null(pch)) {
pch = pch[index]
pt_size = pt_size[index]
pt_gp = subset_gp(pt_gp, index)
l = !is.na(pch)
grid.points(x = rep(0.5, sum(l)), y = y[l], pch = pch[l], size = pt_size[l],
gp = subset_gp(pt_gp, which(l)))
Expand All @@ -242,16 +245,20 @@ anno_simple = function(x, col, na_col = "grey",
if(!is.null(pch)) {
pch = pch[index, , drop = FALSE]
l = !is.na(pch[, i])
grid.points(x[l], y = rep((nc-i +0.5)/nc, sum(l)), pch = pch[l, i], size = pt_size, gp = pt_gp)
grid.points(x[l], y = rep((nc-i +0.5)/nc, sum(l)), pch = pch[l, i],
size = pt_size[i], gp = subset_gp(pt_gp, i))
}
}
} else {
fill = map_to_colors(color_mapping, value[index])
grid.rect(x, y = 0.5, width = 1/n, height = 1, gp = do.call("gpar", c(list(fill = fill), gp)))
if(!is.null(pch)) {
pch = pch[index]
pt_size = pt_size[index]
pt_gp = subset_gp(pt_gp, index)
l = !is.na(pch)
grid.points(x[l], y = rep(0.5, sum(l)), pch = pch[l], size = pt_size[l], gp = subset_gp(pt_gp, which(l)))
grid.points(x[l], y = rep(0.5, sum(l)), pch = pch[l], size = pt_size[l],
gp = subset_gp(pt_gp, which(l)))
}
}
if(border) grid.rect(gp = gpar(fill = "transparent"))
Expand Down Expand Up @@ -644,6 +651,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
axis_grob = if(axis) construct_axis_grob(axis_param, which, data_scale) else NULL

row_fun = function(index, k = 1, N = 1) {

n = length(index)

pushViewport(viewport(xscale = data_scale, yscale = c(0.5, n+0.5)))
Expand All @@ -653,7 +661,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
default.units = "native", pch = pch[i], size = size[i])
}
} else {
grid.points(value[index], n - seq_along(index) + 1, gp = gp, default.units = "native",
grid.points(value[index], n - seq_along(index) + 1, gp = subset_gp(gp, index), default.units = "native",
pch = pch[index], size = size[index])
}
if(axis_param$side == "top") {
Expand All @@ -667,6 +675,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
}

column_fun = function(index, k = 1, N = 1) {

n = length(index)

pushViewport(viewport(yscale = data_scale, xscale = c(0.5, n+0.5)))
Expand All @@ -675,7 +684,7 @@ anno_points = function(x, which = c("column", "row"), border = TRUE, gp = gpar()
grid.points(seq_along(index), value[index, i], gp = subset_gp(gp, i), default.units = "native", pch = pch[i], size = size[i])
}
} else {
grid.points(seq_along(index), value[index], gp = gp, default.units = "native", pch = pch[index], size = size[index])
grid.points(seq_along(index), value[index], gp = subset_gp(gp, index), default.units = "native", pch = pch[index], size = size[index])
}
if(axis_param$side == "left") {
if(k > 1) axis = FALSE
Expand Down Expand Up @@ -857,7 +866,7 @@ anno_lines = function(x, which = c("column", "row"), border = TRUE, gp = gpar(),
grid.lines(y, x, gp = gp, default.units = "native")
}
if(add_points) {
grid.points(y, x, gp = gp, default.units = "native",
grid.points(y, x, gp = subset_gp(pt_gp, index), default.units = "native",
pch = pch[index], size = size[index])
}
}
Expand Down Expand Up @@ -904,7 +913,7 @@ anno_lines = function(x, which = c("column", "row"), border = TRUE, gp = gpar(),
grid.lines(x, y, gp = gp, default.units = "native")
}
if(add_points) {
grid.points(seq_along(index), value[index], gp = pt_gp, default.units = "native",
grid.points(seq_along(index), value[index], gp = subset_gp(pt_gp, index), default.units = "native",
pch = pch[index], size = size[index])
}
}
Expand Down Expand Up @@ -1207,6 +1216,8 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,

n = length(index)
gp = subset_gp(gp, index)
pch = pch[index]
size = size[index]
pushViewport(viewport(xscale = data_scale, yscale = c(0.5, n+0.5)))

grid.rect(x = boxplot_stats[2, ], y = n - seq_along(index) + 1,
Expand Down Expand Up @@ -1253,6 +1264,8 @@ anno_boxplot = function(x, which = c("column", "row"), border = TRUE,

n = length(index)
gp = subset_gp(gp, index)
pch = pch[index]
size = size[index]
pushViewport(viewport(xscale = c(0.5, n+0.5), yscale = data_scale))
grid.rect(x = seq_along(index), y = boxplot_stats[2, ],
height = boxplot_stats[4, ] - boxplot_stats[2, ], width = 1*box_width, just = "bottom",
Expand Down Expand Up @@ -1432,7 +1445,7 @@ anno_histogram = function(x, which = c("column", "row"), n_breaks = 11,
pushViewport(viewport(y = unit(0, "npc"), x = unit(i/n, "npc"), width = unit(1/n, "npc"),
just = c("right", "bottom"), xscale = xscale, yscale = yscale))
grid.rect(y = histogram_breaks[[i]][-1], x = 0, height = histogram_breaks[[i]][-1] - histogram_breaks[[i]][-n_breaks],
width = histogram_counts[[i]], just = c("left", "top"), default.units = "native", gp = subset_gp(gp, index[i]))
width = histogram_counts[[i]], just = c("left", "top"), default.units = "native", gp = subset_gp(gp, i))
popViewport()
}
pushViewport(viewport(yscale = yscale))
Expand Down Expand Up @@ -1658,11 +1671,11 @@ anno_density = function(x, which = c("column", "row"),
just = c("right", "bottom"), xscale = xscale, yscale = yscale))
if(type == "lines") {
grid.polygon(y = density_x[[i]], x = density_y[[i]]*joyplot_scale,
default.units = "native", gp = subset_gp(gp, index[i]))
default.units = "native", gp = subset_gp(gp, i))
} else if(type == "violin") {
grid.polygon(y = c(density_x[[i]], rev(density_x[[i]])),
x = c(density_y[[i]], -rev(density_y[[i]])), default.units = "native",
gp = subset_gp(gp, index[i]))
gp = subset_gp(gp, i))
box_stat = boxplot(value[[i]], plot = FALSE)$stat
grid.lines(y = box_stat[1:2, 1], x = c(0, 0), default.units = "native",
gp = subset_gp(gp, i))
Expand Down Expand Up @@ -2007,7 +2020,7 @@ anno_joyplot = function(x, which = c("column", "row"), gp = gpar(fill = "#000000

n = length(index)

gp = subset(gp, index)
gp = subset_gp(gp, index)

for(i in seq_len(n)) {
pushViewport(viewport(y = unit(0, "npc"), x = unit(i/n, "npc"),
Expand Down Expand Up @@ -2155,7 +2168,7 @@ anno_horizon = function(x, which = c("column", "row"),
}

if(which == "column") {
stop_wrap("anno_horizon() does not support column annotation. If you want, please email me.")
stop_wrap("anno_horizon() does not support column annotation.")
}

if(normalize) {
Expand Down
86 changes: 86 additions & 0 deletions tests/testthat/test-AnnotationFunction.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ draw(anno, test = "anno_points")
anno = anno_points(cbind(c(1:5, 1:5), c(5:1, 5:1)), gp = gpar(col = 2:3))
draw(anno, test = "matrix")

anno = anno_points(1:10, gp = gpar(col = rep(2:3, each = 5)), pch = rep(2:3, each = 5))
draw(anno, test = "anno_points")
draw(anno, index = c(1, 3, 5, 7, 9, 2, 4, 6, 8, 10), test = "anno_points")

##### test anno_lines ###
anno = anno_lines(runif(10))
draw(anno, test = "anno_lines")
Expand Down Expand Up @@ -260,3 +264,85 @@ Heatmap(m) + rowAnnotation(mark = anno)

ht_list = Heatmap(m, cluster_rows = F, cluster_columns = F) + rowAnnotation(mark = anno)
draw(ht_list, row_split = c(rep("a", 95), rep("b", 5)))


### graphic parameters after reordering
index = c(1, 3, 5, 7, 9, 2, 4, 6, 8, 10)
anno = anno_simple(1:10, pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
pt_size = unit(1:10, "mm"))
draw(anno, index, test = "a numeric vector")
anno = anno_simple(1:10, pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
pt_size = unit(1:10, "mm"), which = "row")
draw(anno, index, test = "a numeric vector")


anno = anno_points(1:10, pch = 1:10, gp = gpar(col = rep(c(1, 2), each = 5)),
size = unit(1:10, "mm"))
draw(anno, index, test = "a numeric vector")
anno = anno_points(1:10, pch = 1:10, gp = gpar(col = rep(c(1, 2), each = 5)),
size = unit(1:10, "mm"), which = "row")
draw(anno, index, test = "a numeric vector")


anno = anno_lines(sort(runif(10)), pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
size = unit(1:10, "mm"), add_points = TRUE)
draw(anno, index, test = "a numeric vector")
anno = anno_lines(sort(runif(10)), pch = 1:10, pt_gp = gpar(col = rep(c(1, 2), each = 5)),
size = unit(1:10, "mm"), add_points = TRUE, which = "row")
draw(anno, index, test = "a numeric vector")


anno = anno_barplot(1:10, gp = gpar(fill = rep(c(1, 2), each = 5)))
draw(anno, index, test = "a numeric vector")
anno = anno_barplot(1:10, gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
draw(anno, index, test = "a numeric vector")

anno = anno_barplot(cbind(1:10, 10:1), gp = gpar(fill = 1:2))
draw(anno, index, test = "a numeric vector")
anno = anno_barplot(cbind(1:10, 10:1), gp = gpar(fill = 1:2), which = "row")
draw(anno, index, test = "a numeric vector")


m = matrix(rnorm(100), 10)
m = m[, order(apply(m, 2, median))]
anno = anno_boxplot(m, pch = 1:10, gp = gpar(fill = rep(c(1, 2), each = 5)),
size = unit(1:10, "mm"), height = unit(4, "cm"))
draw(anno, index, test = "a numeric vector")
anno = anno_boxplot(t(m), pch = 1:10, gp = gpar(fill = rep(c(1, 2), each = 5)),
size = unit(1:10, "mm"), which = "row", width = unit(4, "cm"))
draw(anno, index, test = "a numeric vector")

anno = anno_histogram(m, gp = gpar(fill = rep(c(1, 2), each = 5)))
draw(anno, index, test = "a numeric vector")
anno = anno_histogram(t(m), gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
draw(anno, index, test = "a numeric vector")

anno = anno_density(m, gp = gpar(fill = rep(c(1, 2), each = 5)))
draw(anno, index, test = "a numeric vector")
anno = anno_density(t(m), gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
draw(anno, index, test = "a numeric vector")


anno = anno_density(m, type = "violin", gp = gpar(fill = rep(c(1, 2), each = 5)))
draw(anno, index, test = "a numeric vector")
anno = anno_density(t(m), type = "violin", gp = gpar(fill = rep(c(1, 2), each = 5)), which = "row")
draw(anno, index, test = "a numeric vector")


anno = anno_text(month.name, gp = gpar(col = rep(c(1, 2), each = 5)))
draw(anno, index, test = "a numeric vector")
anno = anno_text(month.name, gp = gpar(col = rep(c(1, 2), each = 5)), which= "row")
draw(anno, index, test = "a numeric vector")

lt = lapply(1:10, function(x) cumprod(1 + runif(1000, -x/100, x/100)) - 1)
anno = anno_horizon(lt, gp = gpar(pos_fill = rep(c(1, 2), each = 5), neg_fill = rep(c(3, 4), each = 5)), which = "row")
draw(anno, index, test = "a numeric vector")

m = matrix(rnorm(1000), nc = 10)
lt = apply(m, 2, function(x) data.frame(density(x)[c("x", "y")]))
anno = anno_joyplot(lt, gp = gpar(fill = rep(c(1, 2), each = 5)),
width = unit(4, "cm"), which = "row")
draw(anno, index, test = "joyplot")



0 comments on commit c3ef40c

Please sign in to comment.