【发布时间】:2017-09-13 15:12:24
【问题描述】:
我正在使用 grid_arrange_shared_legend 将具有相同图例的图组合成具有一个图例的图。有没有办法稍微编辑函数,使其也共享 x 和 y 轴标签?例如,如果我制作了 3 个图表并想要使用 grid_arrange_shared_legend 制作的这些图表的 3x1 网格,并且想要最底部的共享 x 轴标签和左侧的共享 y 轴标签,我该怎么做? 这是我用于该功能的代码:
grid_arrange_shared_legend <- function(..., ncol=1, mytitle="mytitle") {
plots <- list(...)
g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))[["grobs"]]
legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
lheight <- sum(legend$height)
grid.arrange(
do.call(arrangeGrob, c(ncol=ncol, lapply(plots, function(x)
x + theme(legend.position="none")))),
legend,
ncol = 1,
heights = unit.c(unit(.95, "npc") - lheight, lheight),
top=textGrob(mytitle, gp = gpar(fontsize=16)))
}
【问题讨论】:
-
为什么不使用
?facet_grid() -
您可以取消 x/y 轴标题,然后在
grid.arrangeo 中使用bottom和left参数,或者按照建议使用 faceting ^^