title: How to control xlim with geom_rect() in R with facets?
body: <p><strong>Goal</strong>: Plot a rectangle (<code>geom_rect</code>) that spans the plotting window.</p>
  
  <p><strong>Problem</strong>: Can't get ggplot2 to accept xlims.</p>
  
  <p><strong>Example</strong>:</p>
  
  <pre><code>library(lubridate)
  library(ggplot2)
  
  #Data frame
  df &lt;- data.frame(time=seq(ymd_hm('2016-01-26 08:00'),ymd_hm('2016-01-26 18:00'), length.out = 100), y=rnorm(100), facto=rep(c('a','b'), each=50))
  
  #Call to ggplot
  ggplot(df, aes(time, y))+
    geom_point()+
    facet_wrap(~facto)+
    geom_rect(aes(xmin=ymd_hm('2016-01-26 07:00'), xmax=ymd_hm('2016-01-26 20:00'), ymin=-1, ymax=1), fill='skyblue4', alpha=I(0.3), data=df[1,])
  </code></pre>
  
  <p>In plot below, the rectangle does not span the plotting window (desired outcome).  The wider the rectangle, the wider the window, and the rectangle never reaches the sides.</p>
  
  <p><a href="https://i.stack.imgur.com/wnmly.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wnmly.png" alt="enter image description here"></a></p>
  
  <p>Simple answer seems to be setting the xlims (<a href="https://stackoverflow.com/questions/3606697/how-to-set-limits-for-axes-in-ggplot2-r-plots">How to set limits for axes in ggplot2 R plots?</a>). </p>
  
  <p>However, using <code>xlim</code> doesn't seem to take (arg is ignored), you can't use <code>coord_cartesian</code> with facets, and applying the <code>scale_x_datetime</code> yields unexpected results:</p>
  
  <pre><code>ggplot(df, aes(time, y))+
    geom_point()+
    facet_wrap(~facto)+
    geom_rect(aes(xmin=ymd_hm('2016-01-26 07:00'), xmax=ymd_hm('2016-01-26 20:00'), ymin=-1, ymax=1), fill='skyblue4', alpha=I(0.3), data=df[1,])+
    scale_x_datetime(limits = c(ymd_hm('2016-01-26 08:00'),ymd_hm('2016-01-26 19:00')))
  </code></pre>
  
  <p><strong>Note how x-values are no longer accurately represented</strong>
  <a href="https://i.stack.imgur.com/2uE2f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2uE2f.png" alt="enter image description here"></a></p>
  
  <p><strong>Further attempts</strong>:
  I've tried freeing the scales in the <code>facet_wrap</code> without effect.  Also tried calls to <code>xlim</code> as a separate line and xlim within the call to <code>ggplot</code>, <code>geom_point</code>, and <code>geom_rect</code>.</p>