1

I have some data where the best fitting non-linear regression is the S curve model. I want to plot the S curve in ggplot2 but do not know how to specify this model. I assume I should use the following code but do not know how to specify the method or formula. Can anyone help?

'''geom_smooth(method = XXX, method.args = list(formula = XXX)'''

SoCo
  • 19
  • 3

1 Answers1

1

You can wrap a prediction in geom_function(). Example with a built-in dataset below:

library(ggplot2)

# From the ?nls examples
df <- subset(DNase, Run == 1)
fit <- nls(density ~ SSlogis(log(conc), Asym, xmid, scal), df)

ggplot(df, aes(conc, density)) +
  geom_point() +
  geom_function(
    fun = function(x) {
      predict(fit, newdata = data.frame(conc = x))
    },
    colour = "red",
  ) +
  scale_x_continuous(trans = "log10")

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Thank you!! This is EXACTLY what I was looking for. This was hands down the best solution to the problem after having looked at several other stack overflow solutions. This has to be the best way to plot non-linear fitted models in ggplot. – James Cutler Jun 11 '22 at 14:33