title: Ggploting stat_function with variable coefficients in R
body: <p>I want to plot a number of polynomials in a ggplot together with some data. The polynomials are given as coefficients in a dataframe (c0,c1,c2). I also have the range I want to plot over (minx,maxx). I attempted to solve this with stat_function but get the error message:</p>
  <pre><code>Error in if (empty(data)) { : missing value where TRUE/FALSE needed
  </code></pre>
  <p>I am not sure where the error is. The code compiles until the last line where I request the plot. Previously I attempted to solve this by creating a dataframe and plotting that. However, the resulting curves did not make sense. My original data is big so it's hard to understand what went wrong there.</p>
  <pre><code>library(ggplot2)
  
  poly&lt;-data.frame(c0=c(1,2,1,2),
                   c1=c(2,1,1,1),
                   c2=c(3,4,1,2),
                   minx=c(0,1,0,1),
                   maxx=c(2,3,2,3)
                   )
  
  data&lt;-data.frame(x=c(1,2,3,4,5,3,3,5,3,5),
                   y=c(4,2,5,4,1,5,3,3,3,5),
                  id=c(1,1,1,2,2,2,3,3,4,4),
               group=c(1,1,1,1,1,2,2,2,2,2)
                  )
  
  h&lt;-ggplot()
  for (i in 1:dim(poly)[1]) {
    h&lt;-h+stat_function(fun=function(x){poly$c0[i]+poly$c1[i]*x+poly$c2[i]*x^2},
                       xlim=c(poly$minx[i],poly$maxx[i]))
  }
  h&lt;-h+geom_line(data=data,aes(x=x,y=y,group=as.factor(id)))+facet_wrap(~group)
  h
  </code></pre>