title: Making plot functions with ggplot and aes_string
body: <p>In Hadley Wickham's <strong>ggplot2</strong> book in chapter 10.3, he alludes to making plot functions.  I want to make many similar plots that use faceting, but I cannot refer to a column.  If all my references are in aesthetics then I can use aes_string and everything works.  Facet_wrap seems not to have an analogue.</p>
  
  <pre><code>library(ggplot2)
  data(iris)
  </code></pre>
  
  <p>This is the plot I want to functionalize.  </p>
  
  <pre><code>pl.flower1 &lt;- ggplot(data=iris, 
                      aes_string(x='Sepal.Length', y='Sepal.Width', color='Petal.Length')) +
                                   geom_point() +facet_wrap(~Species)
  </code></pre>
  
  <p>This works if I do not facet.</p>
  
  <pre><code>flowerPlot &lt;- function(dat, sl, sw, pl, sp){
    ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + geom_point()
  }
  pl.flower2 &lt;- flowerPlot(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length')
  </code></pre>
  
  <p>What should "sp" be two lines below? A formula, a string?  Maybe the whole aproach is wrong.</p>
  
  <pre><code>flowerPlotWrap &lt;- function(dat, sl, sw, pl, sp){
        ggplot(data=dat, aes_string(x=sl, y=sw, color=pl)) + geom_point() +facet_wrap(sp)
      }
      pl.flower3 &lt;- flowerPlotWrap(iris, sl='Sepal.Length', sw='Sepal.Width', pl='Petal.Length', sp= ?????)
  </code></pre>
  
  <p>In addition to an answer I would love pointer on how anyone approaches this problem?</p>