title: ggplot2 + aes_string inside a function via formula interface
body: <p>Interactively, this example works fine:</p>
  
  <pre><code>p &lt;- ggplot(mtcars, aes(mpg, wt)) + geom_point()
  p + facet_grid(. ~ vs)
  </code></pre>
  
  <p>Now, make a function with a formula interface and use <code>aes_string</code> to do this same thing, and it doesn't work (error is: <code>Error in layout_base(data, cols, drop = drop) : At least one layer must contain all variables used for facetting</code>):</p>
  
  <pre><code>tf &lt;- function(formula, data) {
  res &lt;- as.character(formula[[2]])
  fac2 &lt;- as.character(formula[[3]][3])
  fac1 &lt;- as.character(formula[[3]][2])
  
  # p &lt;- ggplot(aes_string(x = fac1, y = res), data = data)
  # p &lt;- p + geom_point() # original attempt
  p &lt;- ggplot() # This is Joran's trick, but it doesn't work here
  p &lt;- p + geom_point(aes_string(x = fac1, y = res), data = data)
  p &lt;- p + facet_grid(.~fac2) # comment this out, and it works but
  # of course is not faceted
  }
  
  p &lt;- tf(formula = wt ~ am*vs, data = mtcars)
  </code></pre>
  
  <p>By Joran's trick I refer to <a href="https://stackoverflow.com/questions/14348781/ggplot2-inside-function-with-a-2nd-aesthetic-scoping-issue">here</a>, which is a similar question I posted recently.  In this case <code>ggplot2</code>doesn't see my faceting request. Making it <code>facet_grid(".~fac2")</code> had no effect.  Suggestions?  I'm continually out-witted by these things.  Thanks!</p>