title: ggplot error " At least one layer must contain all variables used for facetting"
body: <p>I have defined a function in a code chunk in R markdown. The goal is to create a line graph facetted by <code>RACE</code> variable.</p>
  
  <pre><code>{r, Helper function}
    line_graph_gender_race &lt;- 
      function(data_new, xnew, ynew, titlenew, colornew, lwdnew, ylab, wrap) {
        line_graph_gender_race &lt;- 
            ggplot(data = data_new,aes(x = xnew,y = ynew, color = colornew)) + 
            geom_line(lwd = lwdnew) + 
            labs(title = titlenew, y = ylab) + 
            facet_wrap(~wrap, nrow = 1, ncol = 2) + 
            theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
        return (line_graph_gender_race)
      }
  </code></pre>
  
  <p>Next, we call this function in a different chunk;</p>
  
  <pre><code>{r, ref.label="Helper function"}
  
  line_graph_gender_race(
    population_gender_race1_2,
    population_gender_race1_2$YEAR, 
    population_gender_race1_2$TOTAL_POP, 
    "Projected Population as Per Gender from July 1, 2012 through July 1,2060\n(White vs. Black Population)", 
    population_gender_race1_2$SEX, 
    2,  
    "Population Percent", 
    population_gender_race1_2$RACE
  )
  </code></pre>
  
  <p>This facetting variable <code>RACE</code> is present. But we see an error:</p>
  
  <blockquote>
    <p>At least one layer must contain all variables used for facetting</p>
  </blockquote>
  
  <p>How can I solve this?</p>