0

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: enter image description here and p2 and p3 are similarly proprotioned

But - the combo-plot looks like this: enter image description here

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?

user2498193
  • 1,072
  • 2
  • 13
  • 32
  • 1
    Just resize the plot? Or have a look at `?coord_fixed`. – Axeman Nov 24 '16 at 11:23
  • By resize the plot you mean in Paint or Preview or something ? Two problems with that 1) it will stretch/squish text in the plot 2) I have more than one graph to do and want it automated. I did try `coord_fixed` but it didn't work as needed -> `coord_fixed` was over-riding the `coord_cartesian(ylim=c(0,100))` that I had set in my real graph – user2498193 Nov 24 '16 at 11:48
  • I meant resizing the graphics window, or using other dimensions when saving. You can set `ylim` in `coord_fixed` too... – Axeman Nov 24 '16 at 11:54
  • Ahhh right - `coord_fixed` with `ylim` option fixed it actually thanks. If you want to post this is answer I will accept it – user2498193 Nov 24 '16 at 11:57

0 Answers0