所以我在这里使用grid_arrange_shared_legend函数:https://github.com/tidyverse/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs并进行调整以确保有3列 - 这适合我的特定数据集。但是我在保留纵横比方面遇到了问题,而且我的图表正在被压缩。 例如:
library(ggplot2)
library(gridExtra)
library(grid)
grid_arrange_shared_legend_3col <- function(...) {
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(arrangeGrob(grobs=lapply(plots, function(x)
x + theme(legend.position="none")), ncol = 3),
legend,
ncol = 1,
heights = unit.c(unit(1, "npc") - lheight, lheight))
}
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
p1 <- qplot(carat, price, data=dsamp, colour=clarity)
p2 <- qplot(cut, price, data=dsamp, colour=clarity)
p3 <- qplot(color, price, data=dsamp, colour=clarity)
p1 ; p2 ; p3
grid_arrange_shared_legend_3col(p1, p2, p3)
子图非常高!我想知道的是如何在grid_arrange_shared_legend_3col函数中保留原始图的宽高比?