geom_smooth and exponential fits

此生再无相见时 提交于 2019-12-05 06:09:04

As rightly mentioned in the comments, the range of log(y) is 3.19 - 4.09. I think you simply need to bring the fitted values back to the same scale as y so try this. Hopefully helps...

library(ggplot2)

df <- read.csv("test.csv")

linear.model <-lm(y ~ x, df)
log.model <-lm(log(y) ~ x, df)
exp.model <-lm(y ~ exp(x), df)

log.model.df <- data.frame(x = df$x,
                           y = exp(fitted(log.model)))

ggplot(df, aes(x=x, y=y)) + 
  geom_point() +
  geom_smooth(method="lm", aes(color="Exp Model"), formula= (y ~ exp(x)), se=FALSE, linetype = 1) +
  geom_line(data = log.model.df, aes(x, y, color = "Log Model"), size = 1, linetype = 2) + 
  guides(color = guide_legend("Model Type"))

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!