Skip to content

R: plotting multiple functions

4 messages · Allan Clark, Peter Wolf, Martin Maechler +1 more

#
Hi all

Another simple question.

I would like to plot three graphs one the same plot with different
colours. Say red, blue and black. Here are the functions.

r1<-1+5*cos(2*pi*seq(1:100)/20)+rnorm(100)
r2<-1+7*sin(2*pi*seq(1:100)/20)+rnorm(100)
r3<-1+7*sin(2*pi*seq(1:100)/20)+5*cos(2*pi*seq(1:100)/20)+rnorm(100)

Regards
Allan
#
allan clark wrote:

            
Try:

r1<-1+5*cos(2*pi*seq(1:100)/20)+rnorm(100)
r2<-1+7*sin(2*pi*seq(1:100)/20)+rnorm(100)
r3<-1+7*sin(2*pi*seq(1:100)/20)+5*cos(2*pi*seq(1:100)/20)+rnorm(100)

x <- 1:100
y.min<-min(r1,r2,r3)
y.max<-max(r1,r2,r3)
plot(x,type="n",main="3 graphs", xlab="x",ylab="",ylim=c(y.min,y.max))
lines(x,r1,col="red")
lines(x,r2,col="blue")
lines(x,r3,col="black")
vp<-par()$usr
legend(vp[1]+0.85*(vp[2]-vp[1]),
              vp[3]+0.9*(vp[4]-vp[3]),
              legend=c("graph1","graph2","graph3"),             
              col=c("red","blue","black"),
              lty=rep(1,3),
              bty="n")


Peter
#
allan> Hi all Another simple question.

    allan> I would like to plot three graphs one the same plot
    allan> with different colours. Say red, blue and black. Here
    allan> are the functions.

    allan> r1<-1+5*cos(2*pi*seq(1:100)/20)+rnorm(100)
    allan> r2<-1+7*sin(2*pi*seq(1:100)/20)+rnorm(100)
    allan> r3<-1+7*sin(2*pi*seq(1:100)/20)+5*cos(2*pi*seq(1:100)/20)+rnorm(100)

Use matplot()  and save intermediate things such as in


n <- 100
x <- 2*pi* seq(1:n)/20
mx <- cbind(r1 = 1+5*cos(x) + rnorm(n),
            r2 = 1+7*sin(x) + rnorm(n),
            r3 = 1+7*sin(x)+5*cos(x)+rnorm(n))

matplot(x, mx, type = 'b')
# or if you really want other colours (and no points, e.g.)
matplot(x, mx, type = 'l', col = c("red", "blue", "black"))

Regards,
Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
#
On Tue, Feb 03, 2004 at 09:59:31AM +0200, allan clark wrote:

            
plot(r1, type = "n")
points(r1, col = "red", pch = 19)  ## see also ?lines
points(r2, col = "blue", pch = 19)
points(r3, col = "black", pch = 19)