[R] add lowess predicted line to scatter plot

Greg Snow 538280 at gmail.com
Mon Sep 24 19:46:02 CEST 2012


Is GNI sorted?  if not then the lines function plots the line segments
to the points in the order given and that would explain part of the
strangeness (the png file did not make it through).  Are there gaps
between the GNI values?  even if GNI is sorted, your code below will
just draw line segments between values that GNI was observed at and
not a smooth curve between them.

Better would be something like:

xx <- seq( min(GNI), max(GNI), length.out=250 )
y.predict <- predict(y.loess, newdata=data.frame(GNI=xx))
lines(xx,y.predict)

On Mon, Sep 24, 2012 at 2:58 AM, Maximilian Lklweryc
<maxlklweryc at gmail.com> wrote:
> Hi,
> I have a scatter plot of the variables GNI and Lifeexp (Gross National
> Income and Life Expectancy, both metric). So I plotted them and I want to
> add a regression line and a lowess line. I use lowess and not loess because
> I have missing values. My code:
> plot(GNI,Lifeexp)
> abline(lm(Lifeexp~GNI), col="red")
> y.loess<-loess(Lifeexp~GNI,na.
> action = na.exclude)
> y.predict<-predict(y.loess)
> lines(GNI,y.predict)
> But when I do this (I get no error messages) the plot looks strange. The
> lowess line is not a blue smoothy line, but it looks  really strange, I
> attached a png screenshot.
>
> Where is my mistake? Thanks a lot.
>
>         [[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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com




More information about the R-help mailing list