1

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

R - ggplot2 在线性区域外推回归线

使用 stat_function() 在 R 中的 ggplot 中绘制大量自定义函数

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

等效于 ggplot 的 curve()

我对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)
4

1 回答 1

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=50。xdfmax(df$temp.) = 50

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

于 2014-02-12T07:06:58.483 回答