title: How to get the grid of 2 graphs with different geom match with ggplot2
body: <p>Using <code>graphics</code> it is fairly easy to plot 2 graphs with the same x-axis playing with <code>mar</code>
  I would like to reproduce with <code>ggplot2</code>, but it seems difficult. </p>
  
  <p><strong>What I want</strong>:
  I would like to plot a <code>geom_line</code> (several lines), and directly bellow (so no axis/ticks/...) a <code>geom_bar</code> (with same group) that would display the axis with axis/grid perfectly aligned </p>
  
  <p><strong>Data:</strong></p>
  
  <pre><code>library(data.table)
  DT &lt;- data.table( y = runif(400, max = 2), 
                        grp = sample(c('M', 'F'),size = 400, replace = T), 
                        x = rep(as.Date(1:10,origin='2011-01-01'), each = 40)
      )
  TS &lt;- DT[,.(y=sum(y[1L])),.(x,grp)][,y:=cumsum(y),.(grp)]
  #make a "hole" so it is not too easy
  BC &lt;- DT[,.(y=uniqueN(y)),.(x,grp)][x!='2011-01-05']
  #join so the table is the same
  plotData &lt;- merge(TS,BC,by=c('x','grp'),all=T,suffixes=c('_ts','_bc'))
  </code></pre>
  
  <p>Looks like:</p>
  
  <pre><code>             x grp      y_ts y_bc
   1: 2011-01-02   F  1.196320   20
   2: 2011-01-02   M  1.603109   20
   3: 2011-01-03   F  2.549560   26
   4: 2011-01-03   M  3.433326   14
   5: 2011-01-04   F  2.755620   22
   6: 2011-01-04   M  4.819931   18
   7: 2011-01-05   F  3.492297   NA
   8: 2011-01-05   M  6.431233   NA
   9: 2011-01-06   F  4.236778   18
  10: 2011-01-06   M  7.008271   22
  11: 2011-01-07   F  5.207380   18
  12: 2011-01-07   M  8.636382   22
  13: 2011-01-08   F  5.441116   19
  14: 2011-01-08   M  9.462970   21
  ...
  </code></pre>
  
  <p><strong>With grid.arrange:</strong></p>
  
  <pre><code>library(ggplot2)
  library(gridExtra)
  # first graph
  ts &lt;- ggplot(plotData) + aes(x=x,y=y_ts,color=grp) + geom_line() 
  #remove axis etc...
  ts &lt;- ts + theme(axis.ticks.x = element_blank(), axis.text.x = element_blank(), axis.title.x = element_blank())
  # second graph
  bc &lt;- ggplot(plotData) + aes(x=x,y=y_bc,group=interaction(x,grp),fill=grp) 
  bc &lt;- bc + geom_bar(position='dodge',stat='identity')
  #arrange
  grid.arrange(ts, bc , ncol=1,  layout_matrix = matrix(c(1,2),nrow=2), heights=c(2,1))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/k9tnw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k9tnw.png" alt="enter image description here"></a></p>
  
  <p>But clearly BOTH plots and grid are still <strong><em>miss-aligned</em></strong></p>
  
  <p><strong>with gtable: only grid is wrong</strong></p>
  
  <pre><code>require(ggplot2)
  require(gtable)
  #Extract Grobs
  g1&lt;-ggplotGrob(ts)
  g2&lt;-ggplotGrob(bc)
  #Bind the tables
  g&lt;-gtable:::rbind_gtable(g1, g2, 'first')
  #Remove a row between the plots
  g &lt;- gtable_add_rows(g, unit(0,"line"), pos=nrow(g1))
  #draw
  grid.newpage()
  grid.draw(g)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/b4PbW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b4PbW.png" alt="enter image description here"></a></p>