【问题标题】:Stat_function range truncatedStat_function 范围被截断
【发布时间】:2014-02-12 01:23:06
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

    标签: r plot ggplot2 nls


    【解决方案1】:

    试试这个:

    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(...) 在默认数据集df 给定的范围内评估x 的函数。从max(df$temp.) = 50 开始,该函数仅计算为 x=50。

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

    【讨论】:

    • 感谢您的建议!给其他尝试做类似事情的人的一张便条……我不得不替换“临时”。在您的代码中使用“x”(如下所示)以使其重现您显示的图形。再次感谢。 d4+geom_line(aes(x,y,group=NULL),data=df.new)+ + xlim(20,60)+ylim(-.1,1)
    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 2012-02-04
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-23
    • 2023-01-06
    • 1970-01-01
    相关资源
    最近更新 更多