在 geom_smooth 内使用 `nlsfit` 添加指数线来绘图
Use `nlsfit` within geom_smooth to add exponential line to plot
如果可能的话,我想将 easynls
package 中的 nlsfit
与 ggplot2 一起使用。
这是我到目前为止所做的:
设置子集数据:
library('ggplot2')
library('easynls')
x <- seq(25,97)
y <- c(0.014, 0.016, 0.015, 0.016, 0.018, 0.019, 0.023, 0.019, 0.021, 0.017, 0.018, 0.016, 0.016, 0.020, 0.018, 0.019, 0.022, 0.023, 0.027, 0.027, 0.028, 0.031, 0.029, 0.032, 0.030, 0.030, 0.030, 0.033, 0.039, 0.038, 0.039, 0.046, 0.042, 0.043, 0.050, 0.054, 0.059, 0.064, 0.062, 0.058, 0.063, 0.069, 0.071, 0.069, 0.073, 0.071, 0.070, 0.077, 0.086, 0.077, 0.090, 0.086, 0.098, 0.108, 0.112, 0.116, 0.129, 0.120, 0.128, 0.141, 0.150, 0.143, 0.148, 0.150, 0.162, 0.162, 0.168, 0.152, 0.151, 0.161, 0.169, 0.189, 0.184)
data <- data.frame(x,y)
运行 样本数据上的 NLSfit
nlsfit = nlsfit(data.frame(x,y), model=6, start=c(250,0.05))
nlsfit
# $Model
# [1] "y~a*exp(b*x)"
# $Parameters
# y
# coefficient a 0.0061
# coefficient b 0.0358
# p-value t.test for a 0.0000
# p-value t.test for b 0.0000
# r-squared 0.9793
# adjusted r-squared 0.9790
# AIC -500.0812
# BIC -493.2098
使用 plot()
和直线
绘图
plot(x, y)
a <- nlsfit$Parameters[1,]
b <- nlsfit$Parameters[2,]
lines(x, a*exp(x*b), col="steelblue")
尝试将 nls
与 ggplot2 一起使用(这有效 - 但在完整数据集上的拟合效果不佳)...
ggplot(data, aes(x=x, y=y)) + geom_point(
) + geom_smooth(method="nls", formula=y~a*exp(x*b),
method.args=list(start=c(a=250,b=0.05)), se=FALSE)
尝试使用 ggplot2 nlsfit
-- 无效
# Below doesn't work
ggplot(data, aes(x=x, y=y)) + geom_point(
) + geom_smooth(method="nlsfit", formula=y~a*exp(x*b),
method.args=list(data.frame(x, y),
model=6, start=c(250,0.05)), se=FALSE)
# Warning message:
# Computation failed in `stat_smooth()`:
# unused arguments (formula, weights = weight, list(x = 25:97, y = c(0.014, 0.016, 0.015, 0.016, 0.018, 0.019, 0.023, 0.019, 0.021, 0.017, 0.018, 0.016, 0.016, 0.02, 0.018, 0.019, 0.022, 0.023, 0.027, 0.027, 0.028, 0.031, 0.029, 0.032, 0.03, 0.03, 0.03, 0.033, 0.039, 0.038, 0.039, 0.046, 0.042, 0.043, 0.05, 0.054, 0.059, 0.064, 0.062, 0.058, 0.063, 0.069, 0.071, 0.069, 0.073, 0.071, 0.07, 0.077, 0.086, 0.077, 0.09, 0.086, 0.098, 0.108, 0.112, 0.116, 0.129, 0.12, 0.128, 0.141, 0.15, 0.143, 0.148, 0.15, 0.162,
# 0.162, 0.168, 0.152, 0.151, 0.161, 0.169, 0.189, 0.184)))
这可能吗 - 将不胜感激任何帮助。谢谢。
您可以尝试 stat_function
让最后一部分工作:
a <- nlsfit$Parameters[row.names(nlsfit$Parameters) == 'coefficient a',]
b <- nlsfit$Parameters[row.names(nlsfit$Parameters) == 'coefficient b',]
ggplot(data, aes(x=x, y=y)) + geom_point() +
stat_function(fun=function(x) a*exp(b*x), colour = "blue")
如果可能的话,我想将 easynls
package 中的 nlsfit
与 ggplot2 一起使用。
这是我到目前为止所做的:
设置子集数据:
library('ggplot2') library('easynls') x <- seq(25,97) y <- c(0.014, 0.016, 0.015, 0.016, 0.018, 0.019, 0.023, 0.019, 0.021, 0.017, 0.018, 0.016, 0.016, 0.020, 0.018, 0.019, 0.022, 0.023, 0.027, 0.027, 0.028, 0.031, 0.029, 0.032, 0.030, 0.030, 0.030, 0.033, 0.039, 0.038, 0.039, 0.046, 0.042, 0.043, 0.050, 0.054, 0.059, 0.064, 0.062, 0.058, 0.063, 0.069, 0.071, 0.069, 0.073, 0.071, 0.070, 0.077, 0.086, 0.077, 0.090, 0.086, 0.098, 0.108, 0.112, 0.116, 0.129, 0.120, 0.128, 0.141, 0.150, 0.143, 0.148, 0.150, 0.162, 0.162, 0.168, 0.152, 0.151, 0.161, 0.169, 0.189, 0.184) data <- data.frame(x,y)
运行 样本数据上的 NLSfit
nlsfit = nlsfit(data.frame(x,y), model=6, start=c(250,0.05)) nlsfit # $Model # [1] "y~a*exp(b*x)" # $Parameters # y # coefficient a 0.0061 # coefficient b 0.0358 # p-value t.test for a 0.0000 # p-value t.test for b 0.0000 # r-squared 0.9793 # adjusted r-squared 0.9790 # AIC -500.0812 # BIC -493.2098
使用
绘图plot()
和直线plot(x, y) a <- nlsfit$Parameters[1,] b <- nlsfit$Parameters[2,] lines(x, a*exp(x*b), col="steelblue")
尝试将
nls
与 ggplot2 一起使用(这有效 - 但在完整数据集上的拟合效果不佳)...ggplot(data, aes(x=x, y=y)) + geom_point( ) + geom_smooth(method="nls", formula=y~a*exp(x*b), method.args=list(start=c(a=250,b=0.05)), se=FALSE)
尝试使用 ggplot2
nlsfit
-- 无效# Below doesn't work ggplot(data, aes(x=x, y=y)) + geom_point( ) + geom_smooth(method="nlsfit", formula=y~a*exp(x*b), method.args=list(data.frame(x, y), model=6, start=c(250,0.05)), se=FALSE) # Warning message: # Computation failed in `stat_smooth()`: # unused arguments (formula, weights = weight, list(x = 25:97, y = c(0.014, 0.016, 0.015, 0.016, 0.018, 0.019, 0.023, 0.019, 0.021, 0.017, 0.018, 0.016, 0.016, 0.02, 0.018, 0.019, 0.022, 0.023, 0.027, 0.027, 0.028, 0.031, 0.029, 0.032, 0.03, 0.03, 0.03, 0.033, 0.039, 0.038, 0.039, 0.046, 0.042, 0.043, 0.05, 0.054, 0.059, 0.064, 0.062, 0.058, 0.063, 0.069, 0.071, 0.069, 0.073, 0.071, 0.07, 0.077, 0.086, 0.077, 0.09, 0.086, 0.098, 0.108, 0.112, 0.116, 0.129, 0.12, 0.128, 0.141, 0.15, 0.143, 0.148, 0.15, 0.162, # 0.162, 0.168, 0.152, 0.151, 0.161, 0.169, 0.189, 0.184)))
这可能吗 - 将不胜感激任何帮助。谢谢。
您可以尝试 stat_function
让最后一部分工作:
a <- nlsfit$Parameters[row.names(nlsfit$Parameters) == 'coefficient a',]
b <- nlsfit$Parameters[row.names(nlsfit$Parameters) == 'coefficient b',]
ggplot(data, aes(x=x, y=y)) + geom_point() +
stat_function(fun=function(x) a*exp(b*x), colour = "blue")