一些测试数据:GGPLOT2:annotation_custom给出了一个空层
ltd <- data.frame(r = c(rnorm(10), f1 = c(rep("L", 5), rep("H", 5)),
f2 = rep(c("A", "B"), 5))
和最小功能:
tf <- function(formula = NULL, data = NULL) {
res <- as.character(formula[[2]]) # clean & prep data
fac1 <- as.character(formula[[3]][2])
fac2 <- as.character(formula[[3]][3])
counts <- count(data, vars = c(fac2, fac1)) # get table data ready
colnames(counts) <- c(fac2, fac1, "count")
myt <- tableGrob(counts, show.box = TRUE,
show.rownames = FALSE, show.colnames = TRUE,
show.csep = TRUE, show.rsep = TRUE,
separator = "black")
p <- ggplot()
p <- p + geom_point(data = data,
aes_string(x = fac1, y = res, color = fac2, group = fac2))
p <- p + annotation_custom(myt) # comment out and it works
}
运行:
require("plyr")
require("gridExtra")
require("ggplot2")
tmp <- tf(formula = r~f1*f2, data = ltd)
print(tmp)
给人Error in if (nrow(layer_data) == 0) return() : argument is of length zero
如果你打印它存在的tableGrob,所以我不知道这里发生了什么。如果你注释掉了annotation_custom
就行了,我想我正在关注这些文档。谢谢。 (ggplot2_0.9.3)
我的猜测是,'annotation_custom'不喜欢离散轴(如'annotation_raster')。在任何情况下,您都需要指定xmin,xmax,ymin,ymax,但它在这里仍然不起作用。 – baptiste
离散轴无关紧要,我用另一个版本工作。但我正在做一些改变,使用一个简单的公式界面,并符合ggplot2中的最新变化,并且它打破了。 –