【发布时间】:2021-09-20 22:00:25
【问题描述】:
我正在尝试用公式 y=ae^(bx) 注释 ggplot 图
我拟合了一个线性模型log(y) ~ x,并得到了系数的估计值:截距和斜率,以及 r^2 值:
a = round(exp(model$coefficients[1]), 2)
b = round(model$coefficients[2], 2)
r = round(summary(model)$r.squared, 2)
我想将拟合模型的方程作为指数添加到 ggplot 图中,所以假设我的系数是:
a= -2
b= 0.5
r = 0.76
我使用的示例来自:How to add the exponential regression equation and R2 on ggplot graph,它使用以下代码:
eq <- paste0('paste(y, " = ", italic(e^{', round(exp.mod$coefficients[2], 2),
"*x ~~+~~ ", round(exp.mod$coefficients[1], 2),
'}), ~~~~~~~~R^2~ "="~', round(summary(exp.mod)$r.squared, 2), ")")
但这给出了一个有点不同的公式 (y=e^(ax+b)),我尝试重新排列它,但没有一次尝试成功
我也尝试了类似的事情,如下所示:Annotate exponential function ggplot2,但他们的方程式使用常量,我有变量,所以我不知道如何用变量替换数字
最后,为了将方程添加到绘图中,我尝试使用以下行:
ggplot()+
annotate("text", aes(x=3, y=11), label = "y == a*e^{b*x}", parse =TRUE)
【问题讨论】: