title: Pass interactions between variables to a function using dplyr/ggplot
body: <p>I often find it useful to write functions that create a plot using different subsets of a dataset. For example, take data like these:</p>
  
  <pre><code>id &lt;- seq(1:640)
  con1 &lt;- rep(c('hi', 'lo'), each = 320, times = 1)
  con2 &lt;- rep(c('up', 'down'), each = 160, times = 2)
  con3 &lt;- rep(c('fork', 'knife'), each = 80, times = 4)
  con4 &lt;- rep(c('carrot', 'broccoli'), each = 40, times = 8)
  likert &lt;- sample(c(1:5), 640, replace = T)
  
  dat &lt;- as.data.frame(cbind(id, con1, con2, con3, con4, likert))
  </code></pre>
  
  <p>I would like to have a function that would create a facetted plot for any combination of the con1:4 variables that I specify. A simplified version of what I would like looks like this:</p>
  
  <pre><code>groupVar &lt;- 'con1'
  
  plotFunction &lt;- function(groupVar) {
  
    plot &lt;- ggplot(data = dat) +
      geom_bar(aes(x = factor(likert))) +
      facet_wrap(~eval(parse(text = groupVar))) +
      ggtitle(paste('Preferences grouped by ',groupVar)) + 
      theme_bw()
  
    return(plot)
  
  }
  
  plotFunction(groupVar = groupVar)
  </code></pre>
  
  <p>This works for passing single variables, but not when I want to plot an interaction. For example, setting groupVar to 'con1*con2' does not result in the desired result - ie, the plot I would get from manually specifying </p>
  
  <pre><code>facet_wrap(~con1*con2)
  </code></pre>
  
  <p>I understand (no doubt on a fairly superficial level) why this doesn't work, but haven't been able to figure out the solution. My workaround has been to create new variables for each interaction, so that I can then pass the interaction to facet_wrap as a single variable - eg for the con1*con2 interaction:</p>
  
  <pre><code>dat &lt;- dat %&gt;%
    mutate(con1_con2 = paste(con1, con2, sep = '_'))
  
  groupVar &lt;- 'con1_con2'
  
  plotFunction(groupVar = groupVar) 
  </code></pre>
  
  <p>gives the desired result.</p>
  
  <p>I would be grateful for any advice on how I can pass interactions to the function via the groupVar variable (or some other method) so that there is no need to create the new variables.</p>