plotting polynomials

114 views
Skip to first unread message

Andrew Halford

unread,
Nov 23, 2012, 7:42:51 AM11/23/12
to ggp...@googlegroups.com
Hi Listers,

I am a complete beginner with ggplot and only marginally more experienced with R in general.

I have been struggling to produce a nice scatterplot with a 2nd order polynomial regression fitted including 95% confidence intervals. I did get what I wanted under the generic plot function in R but wanted something a bit more publication quality.

Here is what I tried

> qplot(SOI,u_average,data=soi.currents.edited,geom=c('point','smooth'),method='lm',formula=u_average~poly(SOI,2,raw=TRUE))

Error in eval(expr, envir, enclos) : object 'u_average' not found

> soi.currents.edited
      SOI    u_average
1   -28.5 -0.241330663
2   -25.4 -0.251319320
3   -24.4 -0.215724327
4   -24.2 -0.290679083
5   -24.1 -0.155357754
6   -23.5 -0.254288315
7   -22.8 -0.133241939...........

I cannot see why the error message has occurred. The scatterplot comes up OK but no polynomial regression and errors?

Any help appreciated.

Andy

--
Andrew Halford Ph.D
Adjunct Research Scientist
University of Guam & Curtin University
Ph: +61 (0) 468 419 473

Andrew Halford

unread,
Nov 23, 2012, 8:47:34 AM11/23/12
to ggp...@googlegroups.com
Hi All,

I worked out that the use of the generic x and y remains for the call to the model. I have another query moving forward

I am trying to replicate a nice graph that I saw on the website "stack overflow".

I have managed to plot the scatterplot as I want and have overlayed the 2nd order polynomial line but there is no 95% CI. How do I get those on the plot?

 > p <- ggplot (soi.currents.edited,aes(SOI,u_average))
 > p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)
> p <- p+geom_smooth(method='lm',se=TRUE,fill=NA,formula=y~poly(x,2,raw=TRUE),colour='red')

thanks

Andy

Roman Luštrik

unread,
Nov 23, 2012, 8:53:24 AM11/23/12
to Andrew Halford, ggp...@googlegroups.com
What I usually do is calculate the upper and lower bounds of CI and pass those values to geom_smooth. Something along the lines of 

...
geom_smooth(model.pred, aes(ymin = lcl, ymax = ucl, fill = habituation, colour = habituation),
            stat = "identity", alpha = 0.1) +
...

where ymin and ymax represent the aforementioned bounds. Fill and colour are there for making things pretty and stat should be set to "identity" to tell ggplot2 to not bother calculating anything but to take your values. Alpha is set to something rather transparent. model.pred is the data.frame with (at least) columns lcl and ucl.

Cheers,
Roman



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



--
In God we trust, all others bring data.

Ben Bolker

unread,
Nov 23, 2012, 8:55:37 AM11/23/12
to ggp...@googlegroups.com
On 12-11-23 08:47 AM, Andrew Halford wrote:
> Hi All,
>
> I worked out that the use of the generic x and y remains for the call to
> the model. I have another query moving forward
>
> I am trying to replicate a nice graph that I saw on the website "stack
> overflow".
>
> I have managed to plot the scatterplot as I want and have overlayed the
> 2nd order polynomial line but there is no 95% CI. How do I get those on
> the plot?
>
> > p <- ggplot (soi.currents.edited,aes(SOI,u_average))
> > p <- p + geom_point(alpha=2/10, shape=21, fill="blue",
> colour="black", size=5)
>> p <-
> p+geom_smooth(method='lm',se=TRUE,fill=NA,formula=y~poly(x,2,raw=TRUE),colour='red')
>
> thanks
>
> Andy

By setting fill=NA you have turned off the plotting of the confidence
intervals ... try

geom_smooth(method='lm',formula=y~poly(x,2),colour='red')

(se=TRUE is the default anyway, and there's not need to go out of your
way to use a raw polynomial -- the predictions will be the same)

Ben Bolker










>
> On 23 November 2012 20:42, Andrew Halford <andrew....@gmail.com
> Ph: +61 (0) 468 419 473 <tel:%2B61%20%280%29%20468%20419%20473>
>
>
>
>
> --
> Andrew Halford Ph.D
> Adjunct Research Scientist
> University of Guam & Curtin University
> Ph: +61 (0) 468 419 473
>
> --
Reply all
Reply to author
Forward
0 new messages