[R] ggplot: stat_smooth() and nls method

Brian Diggs diggsb at ohsu.edu
Tue Sep 14 21:44:31 CEST 2010

On 9/11/2010 7:52 AM, Benoit Boulinguiez wrote:
> Hi all,
> Does one of you know if there is any way to combine a "nls" method in
> the stat_smooth of ggplot?
> Regards

According to the documentation for predict.nls, it is unable to create 
standard errors for the predictions, so that has to be turned off in the 
stat_smooth call.  Also, the default formula isn't valid for nls and has 
to be overridden, and the start values can be passed.  I used 
geom_smooth rather than stat_smooth, but either work.


DF <- data.frame(x=1:20, y=rnorm(20))

ggplot(DF, aes(x=x, y=y)) +
   geom_smooth(method="nls", formula=y~b*x+c, se=FALSE, 
start=list(b=0,c=1)) +

ggplot(DF, aes(x=x, y=y)) +
   geom_smooth(method="nls", formula=y~sin(b*x), se=FALSE, 
start=list(b=1)) +

Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

More information about the R-help mailing list