Domanda

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:

R - ggplot2 extrapolated regression lines in linear region

Plotting a large number of custom functions in ggplot in R using stat_function()

http://docs.ggplot2.org/0.9.3/stat_function.html

Equivalent of curve() for ggplot

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:

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))

Here is what I have so far for making the plot with ggplot2:

library(ggplot2)
f1 = ggplot(data = df, aes(x = Temp., y = Fv.Fm, group = Tx) )
f2<-f1+
  geom_point(aes(shape=Tx, fill=Tx), size=4)
f3 <- 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 <- 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))

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.

my.nls<-nls(Fv.Fm~a*(-exp(Temp.)+b), data = df, start=list(a=1*10^-20, b=5*10^21))
new<-d4+stat_function(fun=function(x){coef(my.nls)[1]*(-exp(x)+coef(my.nls)[2])})
new

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).

x<-seq(27, 55, length.out = 200)
y <- predict(my.nls,list(Temp. = x))
plot(df$Temp., df$Fv.Fm)
lines(x,y)
È stato utile?

Soluzione

Try this:

df.new=data.frame(x=seq(20,60,0.1),
                  y=coef(my.nls)[1]*(-exp(seq(20,60,0.1))+coef(my.nls)[2]))
d4+geom_line(aes(Temp.,y,group=NULL),data=df.new)+
  xlim(20,60)+ylim(-.1,1)

The problem is that stat_function(...) evaluates the function for x in the range given in your default dataset, df. Since max(df$temp.) = 50, the function is only evaluated out to x=50.

Not a great fit by the way. I'd definitely be looking for a different model.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top