Unable to include exponential function equation within ggplot


I'm currently trying to fit an exponetial curve to a set of data that includes the equation label. I would also like to possibly include an R^2 value and p-value. I know that one way to do this is to use the stat_fit_tidy using a nls. Using the provided mtcars dataset

ggplot(mtcars, aes(x = disp, y = mpg, colour = factor(cyl))) +
  geom_point() +
stat_smooth(method = "nls"
               , formula = y ~ A * exp( B * x)
               , method.args = list( start = c( A = 2, B = 0.2 ))
  stat_fit_tidy(method = "nls", 
                method.args = list(formula = y~A*exp(B*x)),
                size = 3,
                label.x = "center",
                label.y = "bottom",
                parse = TRUE)

I'm struggling to get A and B to be correct values, I keep getting the error
"Warning message:
Computation failed in stat_smooth():
singular gradient "
I think this is because A or B isn't close enough to their actual values. However, even when I try this on other dataset and get A and B correct, I still can't figure out how to use stat_fit_tidy correctly. How would I do this correctly? Is there a easier way of doing this. It's sort of frustrating that this is something that's very simple to do in Excel but complicated in R

Your code is missing ) + after the stat_smooth() function.

The following gives you an example:

I've used the exact user guide that you linked before, but it still can't really figure out how to include the equation for an exponential using that function. This issue hasn't been the missing parathesis (must have copied it wrong).

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.