geom_smooth and exponential fits

前端 未结 1 1180
旧时难觅i
旧时难觅i 2020-12-16 22:02

I am new to R and I\'m having some difficulty plotting an exponential curve using ggplot2. I have a set of data below.

DATA

                


        
相关标签:
1条回答
  • 2020-12-16 22:34

    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"))
    

    0 讨论(0)
提交回复
热议问题