Skip to content

Plotting multiple smooth lines on the same graph

3 messages · Eleni Rapsomaniki, Chuck Cleland, PIKAL Petr

#
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?

scatter.smooth(d1)

Many thanks
Eleni Rapsomaniki
#
Eleni Rapsomaniki wrote:
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")

  
    
#
Hi

you can either use supsmu smoother

plot(d1) # make sure limits for axes are OK (which is not in this 
case and you need to set xlim and ylim accordingly
lines(supsmu(d1[,1], d1[,2]))
points(d2, pch=2) 
lines(supsmu(d2[,1], d2[,2]))

or you can use loess smoother which is used in scatter.smooth and do 
some ordering before plotting and similar plot lines points lines 
construction.

HTH
Petr
On 15 Dec 2006 at 13:42, Eleni Rapsomaniki wrote:
Date sent:      	Fri, 15 Dec 2006 13:42:25 +0000
From:           	Eleni Rapsomaniki <e.rapsomaniki at mail.cryst.bbk.ac.uk>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] Plotting multiple smooth lines on the same graph
Petr Pikal
petr.pikal at precheza.cz