1

I'm having trouble showing the fitted exponential equation on a plot. I used ggplot2 and stat_smooth() to draw the points and fitted lines. Now I want to show the fitted equation on the plot but stat_poly_eq() does not work. Does anyone know how to do that? My code is attached here.

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)
Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
Charlie
  • 11
  • 2

1 Answers1

0

Is this what you're looking for? stat_function should get this done.

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)

enter image description here

TheSciGuy
  • 1,154
  • 11
  • 22
  • Use this as a reference as well: https://rpubs.com/kaz_yos/ggplot2-stat-function – TheSciGuy Nov 18 '20 at 19:45
  • Thank you. But no, the code you provided does not generate the equation. I want to get the fitted equation to show on the plot. The stat_function() failed. – Charlie Nov 18 '20 at 22:59
  • Oh, I must've misread your question. You just want to display the equation. Have you read this SO post: https://stackoverflow.com/questions/38686029/showing-equation-of-nls-model-with-ggpmisc ? – TheSciGuy Nov 19 '20 at 17:06