我正在尝试将非线性曲线拟合到散点图列表中。我使用“ gnls”来拟合非线性模型。 “ gnls”效果很好,但是当我尝试将其用作ggplot2中的平滑方法时。它发出警告:stat_smooth()
中的计算失败:找不到对象'weight'。并且拟合线未在图中显示。这是我的代码:
library(nlme)
library(ggplot2)
Specie <- c(rep("Soybean", 10), rep("Wheat", 10))
Total.biomass <- c(25, 65, 175, 290, 440, 600, 750, 840, 911, 920, 2100, 35, 120, 400, 1000, 1150, 1400, 1600, 1700, 1695)
LMF <- c(0.67,0.6,0.455,0.465,0.415,0.337,0.271,0.220,0.180,0.171,0.115,0.6,0.56,0.47,0.28,0.22,0.19,0.14,0.11,0.115)
dat_LMF <- data.frame(Specie, Total.biomass, LMF)
exp_decay <- function(a, b, x){
return(a*exp(-b*x))
}
fit1 <- gnls(LMF~exp_decay(a, b, Total.biomass), data=dat_LMF,
params = list(a+b~Specie),
start = c(0.6, rep(0, 1),0.0025,rep(0,1)))
ggplot(data = dat_LMF, aes(x=Total.biomass, y=LMF)) +
geom_point()+
facet_wrap(~Specie, scale="free_x")+
stat_smooth(method = "gnls", formula=y~exp_decay(a, b, x),
method.args=list(params = list(a+b~Specie),start=c(0.6, rep(0, 1),0.0025,rep(0,1))), se=F)
有人在ggplot中使用非线性平滑方法有经验吗?谢谢!