Skip to content
Prev 43587 / 398525 Next

R: plotting multiple functions

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