【问题标题】:Add fitted exponential equation into ggplot 2将拟合指数方程添加到 ggplot 2
【发布时间】:2021-03-02 03:00:30
【问题描述】:

我无法在绘图上显示拟合的指数方程。我使用 ggplot2 和 stat_smooth() 来绘制点和拟合线。现在我想在图上显示拟合方程,但 stat_poly_eq() 不起作用。有谁知道这是怎么做到的吗?我的代码附在这里。

mydata = data.frame("conc_ensemble" = c( 2.000000, 2.477121, 2.778151, 2.954243, 3.000000, 3.477121, 3.778151, 3.954243, 4.000000,
                                 4.477121, 4.778151, 4.954243, 5.000000, 5.477121, 5.778151, 5.954243, 6.000000), 
            "REV" = c(5.20000, 3.76000, 2.23000, 1.96000, 1.57500, 1.00000, 0.71250, 0.65000, 0.49000, 0.28300, 0.22900, 0.17500,
                      0.16225, 0.08830, 0.06075, 0.06075, 0.05350))

my.formula = y ~ a*exp(b*x)
ggplot(mydata, aes(conc_ensemble, REV) ) +
  geom_point() +
  stat_smooth(method = 'nls', method.args = list(start = c(a=1, b=1)), 
              formula = my.formula, colour = 'red', se = FALSE) +
  stat_poly_eq(formula = my.formula,
               label.x.npc = 0.2,  size = 3,
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
               parse = TRUE,
               rr.digits = 3)

【问题讨论】:

    标签: r ggplot2 equation non-linear-regression


    【解决方案1】:

    这就是你要找的吗? stat_function 应该完成这项工作。

    library(tidyverse)
    
    mydata = data.frame("conc_ensemble" = c( 2.000000, 2.477121, 2.778151, 2.954243, 3.000000, 3.477121, 3.778151, 3.954243, 4.000000,
                                             4.477121, 4.778151, 4.954243, 5.000000, 5.477121, 5.778151, 5.954243, 6.000000), 
                        "REV" = c(5.20000, 3.76000, 2.23000, 1.96000, 1.57500, 1.00000, 0.71250, 0.65000, 0.49000, 0.28300, 0.22900, 0.17500,
                                  0.16225, 0.08830, 0.06075, 0.06075, 0.05350))
    
    my.formula = y ~ a*exp(b*x)
    
    ggplot(mydata, aes(conc_ensemble, REV) ) +
        geom_point() +
        stat_smooth(method = 'nls', method.args = list(start = c(a=1, b=1)), 
                    formula = my.formula, colour = 'red', se = FALSE) +
        stat_function(fun = my.formula,
                     label.x.npc = 0.2,  size = 3,
                     aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
                     parse = TRUE,
                     rr.digits = 3)
    

    【讨论】:

    猜你喜欢
    • 2021-02-11
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2020-11-10
    • 2021-03-26
    相关资源
    最近更新 更多