[R] Plotting multiple smooth lines on the same graph

Chuck Cleland ccleland at optonline.net
Fri Dec 15 15:14:06 CET 2006


Eleni Rapsomaniki wrote:
> Hi
> 
> Say we have the following data sets:
> d1=cbind(rnorm(100,1,1), rnorm(100,10,50))
> d2=cbind(rnorm(100,5,1), rnorm(100,1,5))
> 
> I want to plot them both on the same graph with a curve fitted for each. 
> I could call scatter.smooth to plot the first curve, but how do I add the
> second?

  I would probably restructure the data and use xyplot() in the lattice
package, but you also could do something along these lines:

d1 <- cbind(rnorm(100), rnorm(100,3,1))
d2 <- cbind(rnorm(100), rnorm(100,1,1))

plot(d1[,1], d1[,2], xlim=range(c(d1[,1], d2[,1])),
                     ylim=range(c(d1[,2], d2[,2])),
                     col="blue", xlab="X", ylab="Y")

points(d2[,1], d2[,2], col="red")
points(loess.smooth(d1[,1], d1[,2]), type="l", col="blue")
points(loess.smooth(d2[,1], d2[,2]), type="l", col="red")

> scatter.smooth(d1)
> 
> Many thanks
> Eleni Rapsomaniki
> 
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list