【问题标题】:Add exponential curve to plot [duplicate]添加指数曲线以绘制[重复]
【发布时间】:2016-10-10 05:39:12
【问题描述】:

我希望在我的绘图中添加一条曲线,以显示随时间呈指数下降。我用基本的 plot() 函数绘制了两组数据,为了清楚起见,我想添加一条平滑线。 两个数据集的数据点是

1.00000 0.37360 0.27688 0.22992 0.17512 0.13768 0.08048
1.00000000 0.44283122 0.30871143 0.23647913 0.22586207 0.09800363 0.06206897

x 值显示随时间的衰减 (0,1,2,3,4,5,6)

【问题讨论】:

  • 您想拟合具有指数衰减的特定模型还是仅拟合非参数平滑线?对于前者,记录您的数据并使用lm,对于后者,请尝试loess
  • 显示指数衰减的特定模型是理想的
  • 如果您想拟合模型,请参阅包中的 nls nlme
  • 欢迎来到 StackOverflow!请考虑提供reproducible example,以便我们更轻松地为您提供帮助。例如,您提供了两组 y 值(我认为),但没有提供 x 值。请提供一些 R 命令,我们可以复制粘贴来重现您的示例。

标签: r plot curve exponential


【解决方案1】:

我喜欢使用 ggplot2,因为它使从拟合模型中添加线条变得如此简单。

没有太多内容,以下内容可能会对您有所帮助....

#prepare the data
y <- c(1.00000, 0.37360, 0.27688, 0.22992, 0.17512, 0.13768, 0.08048,
   1.00000000, 0.44283122, 0.30871143, 0.23647913, 0.22586207, 0.09800363, 0.06206897)
x <- c(0,1,2,3,4,5,6,0,1,2,3,4,5,6)
z <- c(1,1,1,1,1,1,1,0,0,0,0,0,0,0)


dat <- as.data.frame(cbind(x,y,z))

#load the library
library(ggplot2)

#plot the data
ggplot(data=dat,aes(x=x,y=y))+

#add Points with different shapes depending on factor z
geom_point(aes(shape=factor(z)))+

#Add line using non-linear regreassion
stat_smooth(method="nls",formula =  y~a*exp(-x*b),method.args=list(start=c(a=2,b=2)),se=F,color="red")+

#add line using linear regression
stat_smooth(method="lm",formula =  y~exp(-x),se=F,color="blue")

【讨论】:

  • 效果很好!非常感谢米奇。我现在肯定会更多地开始使用 ggplot!
  • 我很高兴它有帮助!
猜你喜欢
  • 2022-01-19
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多