[R] R: plotting multiple functions

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Tue Feb 3 09:50:39 CET 2004


allan clark wrote:

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




More information about the R-help mailing list