Stat_function范围被截断

时间:2014-02-12 01:23:06

标签: r plot ggplot2 nls

我正在尝试使用ggplot2绘制一些数据并拟合非线性曲线。我想将stat_function与我已定义的nls对象一起使用,但结果会产生截断的曲线。我查看了以下页面,但到目前为止我还没有找到解决方案:

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

我对ggplot2不是很有经验,所以如果我错过了一些简单的事情我会道歉,我很感激任何帮助。这是我的示例数据:

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

这是我到目前为止使用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))

所有这些都很有效,但是当我创建一个nls对象并尝试将其与stat_function一起使用时,我得到一条被截断的曲线。我想将曲线向下延伸到x轴。还会产生错误。

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

我想要的是使用基本的r功能更像是以下内容,但是ggplot2提供了所有的铃声和​​口哨声,最终每个治疗(Tx)都有一个nls曲线。

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

1 个答案:

答案 0 :(得分:0)

试试这个:

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)

问题是stat_function(...)会在默认数据集x中给出的范围内评估df的函数。自max(df$temp.) = 50起,该函数仅计算到x = 50。

顺便说一句,不太适合。我肯定会寻找一个不同的模型。