[R] draw two plots on a single panel

Nguyen Dinh Nguyen n.nguyen at garvan.org.au
Thu May 10 00:29:45 CEST 2007


Hi Pat,
Certainly you can
But you need to provide more details, will advise closely
In general, first you plot the “frame” without plotting like this


plot(range(data1$x1,data2$x2), range(data1$y1,data2$y2), type=’n’) # means
just plotting a frame with xaxis ranges from x1-x2 and y from y1-y2, but no
plotting

then depends on your data type (i.e. line or scatter or blah blah) then you
have specific command for them
Give you here an example for reference:

# Two scatter plots in the same graph:

data1 <- data.frame(x1=rnorm(100,70,7), y1=rnorm(100,35,5))
data2 <- data.frame(x2=rnorm(100,78,8),  y2=rnorm(100,40,5))
plot(range(data1$x1,data2$x2), range(data1$y1,data2$y2), type=’n’) 
points(data1$x1,data1$y1,pch=17, col=’blue’)
points(data2$x2,data2$y2,pch=16, col=’red’)


Cheers
Nguyen

Message: 82
Date: Tue, 8 May 2007 16:49:47 -0700 (PDT)
From: "Patrick Wang" <pwang at berkeley.edu>
Subject: [R] draw two plots on a single panel
To: r-help at stat.math.ethz.ch
Message-ID:
	<52196.76.169.69.87.1178668187.squirrel at calmail.berkeley.edu>
Content-Type: text/plain;charset=iso-8859-1

Hi,

I have 2 dataset,

plot(data1)
plot(data2),

but it comes as two graphs, can I draw both on a single panel so I can
compare them?

Thanks
Pat



More information about the R-help mailing list