[R] R: plotting multiple functions

Martin Maechler maechler at stat.math.ethz.ch
Tue Feb 3 09:54:09 CET 2004

>>>>> "allan" == allan clark <allan at stats.uct.ac.za>
>>>>>     on Tue, 03 Feb 2004 09:59:31 +0200 writes:

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

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

More information about the R-help mailing list