So I am using the grid_arrange_shared_legend function from here: https://github.com/tidyverse/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs with an adaption to make sure there are 3 columns - this suits my particular dataset. However I'm having a problem preserving the aspect ratio and my graphs are coming out squished. Example:
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)
So p1 looks like so:
and p2 and p3 are similarly proprotioned
But - the combo-plot looks like this:
The subgraphs are very tall! What I would like to know is how do I preserve the aspect ratio of the original graph within the grid_arrange_shared_legend_3col function?