[R] Plotting data with a fitted curve

hadley wickham h.wickham at gmail.com
Tue Apr 17 02:19:33 CEST 2007


On 4/16/07, Paul Lynch <plynchnlm at gmail.com> wrote:
> Suppose you have a vector of data in x and response values in y.  How
> do you plot together both the points (x,y) and the curve that results
> from the fitted model, if the model is not y ~ x, but a higher order
> polynomial, e.g. y~poly(x,2)?  (In other words, abline doesn't work
> for this case.)

One way is to use ggplot:

install.packages("ggplot")
library(ggplot)

qplot(mpg, wt, data=mtcars, type=c("point","smooth"))
qplot(mpg, wt, data=mtcars, type=c("point","smooth"), method=lm)
qplot(mpg, wt, data=mtcars, type=c("point","smooth"), method=lm,
formula=y~poly(x,2))

library(splines)
qplot(mpg, wt, data=mtcars, type=c("point","smooth"), method=lm,
formula=y~ns(x,2))

Regards,

Hadley



More information about the R-help mailing list