[R] multiple lowess line in one plot

Paul Johnson pauljohn32 at gmail.com
Wed Jan 4 21:15:10 CET 2006


It appears to me lowess has no "subset" or "strata" option. The
brute-force way (which my students like best) is just to create 4
columns, one for each group (with "unstack" or such) and then run one
lines(lowess()) command for each one.

I think there is a bit more art in using by, which will create subsets
on the fly for you.

Here is a self contained example that I just worked out to illustrate.
grp is the grouping factor, x and y are just illustrative data. The by
function creates the data subsets and they are accessed in the FUN as
"sub1".

x <- rnorm(100)
grp <- gl(4,25)
y <- rpois(100, lambda=4)
mydf <- data.frame(x,y,grp)
with(mydf, plot(x,y))
by (mydf, list(grp), function(sub1) lines(lowess(sub1$x, sub1$y)))

Hope that helps

On 1/4/06, Dean Sonneborn <dsonneborn at ucdavis.edu> wrote:
> I'm using this code to plot a smoothed line.  These two columns of data
> really represent 4 groups and I'd like to plot a separate line for each
> group but have them all in the same plot. The R-Docs for lowess do not
> seem to indicate some type of "GROUPS=var_name" option. What would be
> the syntax for this?
>
> plot(AWGT ~ lipid )
> lines(lowess(lipid , AWGT, f=.8))
>
>
> --
> Dean Sonneborn, MS
> Programmer Analyst
> Department of Public Health Sciences
> University of California, Davis
> (530) 754-9516
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>


--
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas




More information about the R-help mailing list