[R] How to plot a Quadratic Model?

Ista Zahn istazahn at gmail.com
Mon Oct 5 18:50:34 CEST 2009


I hardly use base graphics so I'm no help there. You can do this
easily with ggplot2 though:

library(ggplot2)
X <- rnorm(100)
Y <- rnorm(100) - X^2
qplot(x=X, y=Y, geom=c("point", "smooth"), method="lm", formula = y ~
poly(x, 2))

Note that X is not x and Y is not y in the sense that "formula = Y ~
poly(X, 2)" will not work (this tripped me up at first). qplot is
taking x to mean "the first argument" (X in this case) and y to mean
"the second argument" (Y in this case).

-Ista

On Mon, Oct 5, 2009 at 11:42 AM, Juliano van Melis <jvmelis at gmail.com> wrote:
> Good day for all,
>
> I'm a beginner aRgonaut, thus I'm having a problem to plot a quadratic model
> of regression in a plot.
> First I wrote:
>
>>plot(Y~X)
>
> and then I tried:
>
>>abline(lm(Y~X+I(X^2))
>
> but "abline" only uses the first two of three regression coefficients, thus
> I tried:
>
>>line(lm(Y~X+I(X^2))
>
> but a message error is showed ("insufficient observations").
>
> Therefore, I want to know: how could I plot a quadratic line in my plot
> graph?
>
> thanks!
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org




More information about the R-help mailing list