title: Stat_function range truncated
body: <p>I am trying to use ggplot2 to plot some data and fitted non-linear curves.  I want to use stat_function with nls objects that I have already defined, but the result produces a truncated curve.  I have looked at the following pages but so far I haven't found a solution:</p>
  
  <p><a href="https://stackoverflow.com/questions/20905431/r-ggplot2-extrapolated-regression-lines-in-linear-region">R - ggplot2 extrapolated regression lines in linear region</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19846714/plotting-a-large-number-of-custom-functions-in-ggplot-in-r-using-stat-function">Plotting a large number of custom functions in ggplot in R using stat_function()</a></p>
  
  <p><a href="http://docs.ggplot2.org/0.9.3/stat_function.html" rel="nofollow noreferrer">http://docs.ggplot2.org/0.9.3/stat_function.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5177846/equivalent-of-curve-for-ggplot">Equivalent of curve() for ggplot</a></p>
  
  <p>I am not very experienced with ggplot2, so I apologize if I am missing something simple and I'm grateful for any help.  Here my example data:</p>
  
  <pre><code>df=structure(list(Spp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Dugentia", "Eugenia", 
  "Faramea", "Licania", "Mouriri"), class = "factor"), Tx = structure(c(1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Control", 
  "Drought"), class = "factor"), no. = structure(c(1L, 3L, 4L, 
  5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 
  6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 
  5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 
  5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L), .Label = c("1", 
  "101", "2", "3", "4", "5", "6", "7", "7A", "9"), class = "factor"), 
  Fv.Fm = c(0.74, 0.702, 0.797, 0.782, 0.769, 0.759, 0.701, 
  0.805, 0.79, 0.775, 0.763, 0.725, 0.8, 0.786, 0.774, 0.759, 
  0.664, 0.791, 0.776, 0.758, 0.729, 0.592, 0.757, 0.722, 0.681, 
  0.66, 0.084, 0.652, 0.633, 0.63, 0.569, 0.259, 0.424, 0.376, 
  0.432, 0.771, 0.696, 0.685, 0.761, 0.782, 0.772, 0.736, 0.775, 
  0.784, 0.755, 0.707, 0.746, 0.777, 0.765, 0.705, 0.744, 0.706, 
  0.55, 0.582, 0.635, 0.615, 0.384, 0.504, 0.513, 0.584, 0.378, 
  0.328, 0.302), Temp. = c(27L, 27L, 27L, 27L, 27L, 30L, 30L, 
  30L, 30L, 30L, 35L, 35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 
  40L, 45L, 45L, 45L, 45L, 45L, 48L, 48L, 48L, 48L, 48L, 50L, 
  50L, 50L, 50L, 50L, 27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L, 
  35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 45L, 45L, 45L, 45L, 
  48L, 48L, 48L, 48L, 50L, 50L, 50L, 50L)), .Names = c("Spp", 
  "Tx", "no.", "Fv.Fm", "Temp."), class = "data.frame", row.names = c(NA, 
  63L))
  </code></pre>
  
  <p>Here is what I have so far for making the plot with ggplot2:</p>
  
  <pre><code>library(ggplot2)
  f1 = ggplot(data = df, aes(x = Temp., y = Fv.Fm, group = Tx) )
  f2&lt;-f1+
    geom_point(aes(shape=Tx, fill=Tx), size=4)
  f3 &lt;- f2 +  scale_x_continuous("Temperature (°C)", limits=c(25,55)) +
    scale_y_continuous("Fv/Fm", limits = c(0, 1)) +
    scale_shape_manual(values=c(24,21)) +
    scale_fill_manual(values=c("#4D4D4D","#E6E6E6")) +
    theme_bw()
  d4 &lt;- f3 + theme(panel.grid.major = element_blank(), 
                   panel.grid.minor = element_blank(),
                   axis.text.x = element_text(size = 11),
                   axis.text.y = element_text(size = 11),
                   legend.title = element_blank(),
                   legend.text = element_text(size=12))
  </code></pre>
  
  <p>All of that works well, but then when I make an nls object and try to use it with stat_function I get a curve that is truncated.  I would like to extend the curve down to the x-axis.  There is also an error generated.</p>
  
  <pre><code>my.nls&lt;-nls(Fv.Fm~a*(-exp(Temp.)+b), data = df, start=list(a=1*10^-20, b=5*10^21))
  new&lt;-d4+stat_function(fun=function(x){coef(my.nls)[1]*(-exp(x)+coef(my.nls)[2])})
  new
  </code></pre>
  
  <p>What I want is something more like the following using basic r functionality, but with all the bells and whistles offered by ggplot2, and ultimately one nls curve for each treatment (Tx).</p>
  
  <pre><code>x&lt;-seq(27, 55, length.out = 200)
  y &lt;- predict(my.nls,list(Temp. = x))
  plot(df$Temp., df$Fv.Fm)
  lines(x,y)
  </code></pre>