title: writing R function with ggplot
body: <p>I have to plot multiple datasets in the same format, and after copy-pasting the code several times, I decided to write a function.
  I understand simple function in R, and managed to write the following:</p>
  
  <pre><code>testplot &lt;- function(data, mapping){
  output &lt;- ggplot(data) +
    geom_bar(mapping,
             stat="identity", 
               position='stack')
  }
  p &lt;- testplot(df, aes(x=xvar, y=yvar, fill=type))
  </code></pre>
  
  <p>this works fine, however, my plot is more complicated and requires the "data" argument to go separately into each component:</p>
  
  <pre><code>output &lt;- ggplot() +
    geom_bar(df1, mapping,
             stat="identity", 
               position='stack')+
  geom_errorbar(df1, ...)+
  geom+bar(df2, mapping,
  ...+
  geom_errorbar(df2, ...)
  </code></pre>
  
  <p>but when I write the function and try to run it as </p>
  
  <pre><code>output &lt;- ggplot() +
    geom_bar(data, mapping,
             stat="identity", 
               position='stack')
  }
  p &lt;- testplot(df, aes(x=xvar, y=yvar, fill=type))
  </code></pre>
  
  <p>it gives me an error:</p>
  
  <pre><code>Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with class uneval Did you accidentally pass `aes()` to the `data` argument?
  </code></pre>
  
  <p>Is there a way around it?</p>
  
  <p>EDIT: when I try to include 2 dataframes like this:</p>
  
  <pre><code>testplot &lt;- function(data, data2, mapping){
  output &lt;- ggplot() +
   geom_bar(data=data, mapping=mapping,
             stat="identity", 
               position='stack',
             width = barwidth)+
  geom_bar(data2=data2, mapping=mapping,
             stat="identity", 
               position='stack',
             width = barwidth)
  }
  
  
  
  p &lt;- testplot(data=df, data2=df2, mapping=aes(x=norms_number, y=coeff.BLDRT, fill=type))
  </code></pre>
  
  <p>it says "Ignoring unknown parameters: data2"</p>