[R] 2 plots 1 figure

ben@zoo.ufl.edu ben at zoo.ufl.edu
Tue Nov 14 19:53:12 CET 2000

```  par(new=TRUE) will do this but probably isn't what you want unless you
set xlim and ylim so that the graphs are on exactly the same scale.  It
might be better to save the output from the two hist() calls and
then use

x <- rnorm(100)
y <- rnorm(100)
h <- hist(x,xlim=c(-3,3),ylim=c(0,20),
breaks=seq(-3,3,length=30),col="blue")
par(new=TRUE)
hist(y,xlim=c(-3,3),ylim=c(0,20),breaks=h\$breaks,ann=FALSE,col="red")

but it might be better to use barplot() to put the histograms side by
side:

barplot(rbind(h\$counts,h2\$counts),beside=TRUE)

In the newer versions of R hist() is a little bit more flexible,
returning an object of type "histogram":  eventually it might be worth
writing a "multihist" type as well, since this comparison of distributions
is something that people want to do a lot in exploratory data analysis.

On Tue, 14 Nov 2000, Meriema Belaidouni wrote:

>
>
>
> In fact my question is more general perhaps this example is better
>  x and y are some variables
>
> hist(x)
> hist(y)
>
> my question is how one can get both histograms in the same figure so that they can
> cross each others.
>
> This command exist under matlab it is hold on.
>
>
>
> Torsten Hothorn a écrit:
>
> > On Tue, 14 Nov 2000, Meriema Belaidouni wrote:
> >
> > > How do you obtain two plots on the same figure?
> > > for example
> > > plot(rnorm(100)
> > > plot(rnorm(100),type="l")
> >
> > Does
> >
> > x <- rnorm(100)
> > plot(x)
> > lines(x)                # or/and ...
> > points(x, pch=5)
> >
> > do what you want?
> >
> > Torsten
> >
> > >
> > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> > > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> > > Send "info", "help", or "[un]subscribe"
> > > (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> > >
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

--
318 Carr Hall                                bolker at zoo.ufl.edu
Zoology Department, University of Florida    http://www.zoo.ufl.edu/bolker
Box 118525                                   (ph)  352-392-5697
Gainesville, FL 32611-8525                   (fax) 352-392-3704

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```