Re: [R] How to plot two graphs on one single plot?

From: Steven McKinney <smckinney_at_bccrc.ca>
Date: Sat 24 Feb 2007 - 00:01:53 GMT

Broadly speaking, there are a few classes of plotting functions.

  1. "New graph" functions. The plot() function starts a new graph.
  2. "Add to a graph" functions The points(), lines(), text() etc. functions add something to the current graph.
  3. "Control graph" functions par() controls various aspects of the graph.

R graphics experts might have some better classification and terminology.

So you want your second plotting function to be points() rather than plot(), to add to the existing graph.

Try

> x1=rnorm(25, mean=0, sd=1)
> y1=dnorm(x1, mean=0, sd=1)
>
> x2=rnorm(25, mean=0, sd=1)
> y2=dnorm(x2, mean=0, sd=1)
> plot(x1, y1, type='p', xlim=range(x1,x2), ylim=range(y1, y2), xlab='x', ylab='y')
> points(x2, y2, type='p', col="red", xlab='x', ylab='y')
>

Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program British Columbia Cancer Research Centre

email: smckinney@bccrc.ca

tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C.
V5Z 1L3
Canada

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch on behalf of Yun Zhang Sent: Fri 2/23/2007 7:34 AM
To: Henrique Dallazuanna
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] How to plot two graphs on one single plot?  

Thanks. Now R plots two graphs on one plot. Yet they are still on two graphs, vertically parallelized with each other.

But what I want to do is actually plotting two distribution on one single graph, using the same x and y axis. Like:

|
|
|               (dist2)
|   (dist 1)
|
--------------------------->

Is it possible to do that?

Thanks,
Yun

Henrique Dallazuanna wrote:
> par(mfrow=c(2,1))
> #your plot
> #after plot
> par(mfrow=c(1,1))

>

> On 23/02/07, *Yun Zhang* <yunzhang@princeton.edu
> <mailto:yunzhang@princeton.edu>> wrote:
>

> Hi,
>
> I am trying to plot two distribution graph on one plot. But I dont
> know
> how. I set them to the same x, y limit, even same x, y labels.
>
> Code:
> x1=rnorm(25, mean=0, sd=1)
> y1=dnorm(x1, mean=0, sd=1)
>
> x2=rnorm(25, mean=0, sd=1)
> y2=dnorm(x2, mean=0, sd=1)
> plot(x1, y1, type='p', xlim=range(x1,x2), ylim=range(y1, y2),
> xlab='x',
> ylab='y')
> plot(x2, y2, type='p', col="red", xlab='x', ylab='y')

>
> They just dont show up in one plot.
>
> Any hint will be very helpful.
>
> Thanks,
> Yun
>
> ______________________________________________
> R-help@stat.math.ethz.ch <mailto:R-help@stat.math.ethz.ch> mailing
> list
> https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> > > >

> --
> Henrique Dallazuanna
> Curitiba-Paraná
> Brasil


R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat Feb 24 11:14:25 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 24 Feb 2007 - 11:30:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.