title: How to align multiple ggplot2 plots and add shadows over all of them
body: <p><strong><em><a href="https://www.dropbox.com/s/caftw7aiudzy9y1/SF_Question.csv" rel="nofollow noreferrer">Please download the data here!</a></em></strong></p>
  
  <p><strong>Target:</strong> Plot an image like this:</p>
  
  <p><img src="https://i.stack.imgur.com/WCb8T.png" alt="this"> </p>
  
  <p><strong>Features:</strong>
   1. two different time series;
   2. the lower panel has a reverse y-axis;
   3. shadows over two plots.</p>
  
  <p><strong>Possible solutions:</strong><br>
  1. Facetting is not appropriate - (1) can not just make one facet's y axis reverse while keep the other(s) unchanges. (2) difficult to adjust the individual facets one by one.<br>
  2. Using viewports to arrange individual plots using the following codes:</p>
  
  <pre><code>library(ggplot2)
  library(grid)
  library(gridExtra)
  
  ##Import data
  df&lt;- read.csv("D:\\R\\SF_Question.csv")
  
  ##Draw individual plots
  #the lower panel
  p1&lt;- ggplot(df, aes(TIME1, VARIABLE1)) + geom_line() + scale_y_reverse() + labs(x="AGE") + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000))
  #the upper panel
  p2&lt;- ggplot(df, aes(TIME2, V2)) + geom_line() + labs(x=NULL) + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000)) + theme(axis.text.x=element_blank())
  
  ##For the shadows
  #shadow position
  rects&lt;- data.frame(x1=c(1100,1800),x2=c(1300,1850),y1=c(0,0),y2=c(100,100))
  #make shadows clean (hide axis, ticks, labels, background and grids)
  xquiet &lt;- scale_x_continuous("", breaks = NULL)
  yquiet &lt;- scale_y_continuous("", breaks = NULL)
  bgquiet&lt;- theme(panel.background = element_rect(fill = "transparent", colour = NA))
  plotquiet&lt;- theme(plot.background = element_rect(fill = "transparent", colour = NA))
  quiet &lt;- list(xquiet, yquiet, bgquiet, plotquiet)
  prects&lt;- ggplot(rects,aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2))+ geom_rect(alpha=0.1,fill="blue") + coord_cartesian(xlim = c(1000, 2000)) + quiet
  
  ##Arrange plots
  pushViewport(viewport(layout = grid.layout(2, 1)))
  vplayout &lt;- function(x, y) 
    viewport(layout.pos.row = x, layout.pos.col = y)
  #arrange time series
  print(p2, vp = vplayout(1, 1))
  print(p1, vp = vplayout(2, 1))
  #arrange shadows
  print(prects, vp=vplayout(1:2,1))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/AONz5.png" alt="this"> </p>
  
  <p><strong>Problems:</strong></p>
  
  <ol>
  <li>the x-axis doesn't align correctly;</li>
  <li>the shadow locations are wrong (because of the incorrect line-up of x-axis).</li>
  </ol>
  
  <p>After Googling all around:</p>
  
  <ol>
  <li>I firstly noticed that "align.plots() from ggExtra" could do this job. However, it has been deprecated by the author; </li>
  <li>Then I've tried the <a href="https://stackoverflow.com/questions/9198637/top-to-bottom-alignment-of-two-ggpot2-figures">gglayout solution</a>, but no luck - I even could not install the "cutting-edge" package; </li>
  <li><p>Finally, I've tried the <a href="https://stackoverflow.com/questions/13656642/r-align-plot-areas-in-ggplot">gtable solution</a> using the following code:  </p>
  
  <pre><code>gp1&lt;- ggplot_gtable(ggplot_build(p1))
  gp2&lt;- ggplot_gtable(ggplot_build(p2))
  gprects&lt;- ggplot_gtable(ggplot_build(prects))
  maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3], gprects$widths[2:3])
  gp1$widths[2:3] &lt;- maxWidth
  gp2$widths[2:3] &lt;- maxWidth
  gprects$widths[2:3] &lt;- maxWidth
  grid.arrange(gp2, gp1, gprects)
  </code></pre></li>
  </ol>
  
  <p><img src="https://i.stack.imgur.com/4vJVb.png" alt="this"> </p>
  
  <p>Now, the x-axis of upper and lower panel do align correctly. But the shadow positions are still wrong. And more importantly, I can not overlap the shadow plot on the two time-series. After several day's attempts, I nearly give up...</p>
  
  <p>Could somebody here give me a hand?</p>