0

I'm using nls with geom_smooth as below. This works fine

ggplot(model.data, aes(x=T, y=Value)) +
    geom_line(aes(colour=as.factor(Year)), size = 1) +
    geom_smooth(method = "nls", formula = y ~ a * (1 - exp(-b * x)), method.args = list(start = c(a=-2000,b=0.005)), se=FALSE, size=2, color='Grey')

But I also want to reverse the x-axis so I add

scale_x_reverse()

This then results in errors from nls, i.e.

Warning message: Computation failed in stat_smooth(): number of iterations exceeded maximum of 50

So it appears that scale_x_reverse() is transforming the x values passed to geom_smooth()? How can I express the model so it works regardless of the axis transformation?

Psidom
  • 209,562
  • 33
  • 339
  • 356
David Waterworth
  • 2,214
  • 1
  • 21
  • 41
  • 3
    I can't reproduce this problem using the `mtcars` data frame. It will be easier to help you if you provide a sample of `model.data`. Paste into your question the output of `dput(model.data)`. It doesn't have to be the whole data frame, but the data sample should produce the same error you're getting. – eipi10 Jul 07 '16 at 05:22
  • It looks like you are getting a "warning" not an "error". Is it still plotting it ? – steveb Jul 07 '16 at 07:14
  • One solution is to pre-calculate the model fit, make predictions and then use geom_line to plot these – Richard Telford Jul 07 '16 at 09:46
  • @steveb it's not plotting, sometimes I get an error sometimes a warning. Basically once the scale_x_reverse() is added nls fails to converge and nothing is plotted – David Waterworth Jul 08 '16 at 01:53
  • @Richard Telford thanks, yes I've done that but I was trying to understand a little more what's going on and is there a general way of dealing with this issue – David Waterworth Jul 08 '16 at 01:54
  • @aosmith thanks, I'll look into that – David Waterworth Jul 08 '16 at 01:54
  • @eipi10 I'll try and create a reproducible sample – David Waterworth Jul 08 '16 at 01:55

0 Answers0