title: ggplot2 grid_arrange_shared_legend change number of columns
body: <p>I am using <code>grid_arrange_shared_legend</code>, which I found <a href="https://github.com/hadley/ggplot2/wiki/Share-a-legend-between-two-ggplot2-graphs" rel="nofollow">here</a> to generate a grid of plots. I slightly modified <code>grid_arrange_shared_legend</code> to fit my needs:</p>
  
  <pre><code>grid_arrange_shared_legend &lt;- function(plots) {
    g &lt;- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
    legend &lt;- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
    lheight &lt;- sum(legend$height)
    grid.arrange(
      do.call(arrangeGrob, lapply(plots, function(x)
        x + theme(legend.position="none"))),
      legend,
      ncol = 1,
      heights = unit.c(unit(1, "npc") - lheight, lheight))
  }
  </code></pre>
  
  <p>I call this function like that</p>
  
  <pre><code>pdf("plots.pdf",width=12.21/2.54, height=20.92/2.54)
  grid_arrange_shared_legend(plotList) #ncol=length(ns), main = "Main title")
  dev.off()
  </code></pre>
  
  <p>This generates a pdf file with 4 columns of plots (see <a href="http://www.docdroid.net/LTU3fOc/plots.pdf.html" rel="nofollow">pdf1</a>). However, I need 3 columns instead. I tried achieving this by replacing <code>ncol=1</code> with <code>ncol=3</code> in <code>grid_arrange_shared_legend</code>. This results in all the plots being in the left column and the middle and right column being empty (see <a href="http://docdro.id/BFugdqa" rel="nofollow">pdf2</a>).</p>
  
  <p><strong>How can I achieve a 3 column plot?</strong></p>