[R] ggplot: stat_smooth() and nls method

Benoit Boulinguiez benoit.boulinguiez at ensc-rennes.fr
Wed Sep 15 11:40:15 CEST 2010


great help, thanks a lot

Le 14/09/2010 21:44, Brian Diggs a écrit :
> 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.
>
>
> library("ggplot2")
>
> 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)) +
>   geom_point()
>
> ggplot(DF, aes(x=x, y=y)) +
>   geom_smooth(method="nls", formula=y~sin(b*x), se=FALSE, 
> start=list(b=1)) +
>   geom_point()
>



More information about the R-help mailing list