title: The perils of aligning plots in ggplot
body: <p><strong>QUESTION</strong></p>
  
  <p>How do you combine separate plots (ggplot2), with different y-axis and different plot heights, yet retain alignment? </p>
  
  <p><strong>DETAIL</strong></p>
  
  <p>When combining plots with <code>grid.arrange</code> (method1), with different y-axis units, they do not align. One way around this is to use <code>gtable</code> (method2), but I cannot adjust the relative height of the plots. </p>
  
  <p><strong>EXAMPLE</strong></p>
  
  <pre><code>require(ggplot2)
  
  #Make two plots, with different y axis
    x = c(1, 5)
    y= c(.1, .4)
    data1&lt;-data.frame(x,y)
    top&lt;-
      ggplot(data1, aes(x=x, y=y))+
      geom_line()
  
    x = c(1, 5)
    y= c(100000, 400000)
    data2&lt;-data.frame(x,y)
    bottom&lt;-
      ggplot(data2, aes(x=x, y=y))+
      geom_line()
  
  
  # Method 1 - Grid Extra 
    require(gridExtra)
    grid.arrange(top, bottom, heights=c(.6,.3))
  </code></pre>
  
  <p>Method 1 results in this plot, which is misaligned due to the different length y axis labels: </p>
  
  <p><img src="https://i.stack.imgur.com/vbwPp.png" alt="enter image description here"></p>
  
  <pre><code>#Method 2 - gtable
    require(gtable)
    #Extract Grobs
    g1&lt;-ggplotGrob(top)
    g2&lt;-ggplotGrob(bottom)
    #Bind the tables
    g&lt;-gtable:::rbind_gtable(g1, g2, "first")
    #Remove a row between the plots
    g &lt;- gtable_add_rows(g, unit(-1,"cm"), pos=nrow(g1))
    #draw
    grid.newpage()
    grid.draw(g)
  </code></pre>
  
  <p>Method 2 results in aligned plots, but I cannot adjust the height of each plot.
  <img src="https://i.stack.imgur.com/IwnhR.png" alt="enter image description here"></p>
  
  <p><strong>THANKS!</strong></p>