Skip to content

2 plots 1 figure

6 messages · Meriema Belaidouni, Kjetil Kjernsmo, Ben Bolker +3 more

#
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:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Tue, 14 Nov 2000, Meriema Belaidouni wrote:

            
I've hacked up something that does this, it's undocumented, not
extensively tested, and is not very elegant, but it works for my
purpose, and I guess it might work for your purpose too. The
easiest way to get it, including my comments, is to look in Roberts
R-devel-archives, here:
<URL:http://www.ens.gu.edu.au/robertk/R/devel/00b/0024.html>

After evaluating this code, what you would do to use it is:
hist(x)
lines(hist(y, plot=FALSE))

Best,

Kjetil
#
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:

            

  
    
#

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

    Kjetil> I've hacked up something that does this, it's undocumented, not
    Kjetil> extensively tested, and is not very elegant, but it works for my
    Kjetil> purpose, and I guess it might work for your purpose too. The
    Kjetil> easiest way to get it, including my comments, is to look in Roberts
    Kjetil> R-devel-archives, here:

        <URL:http://www.ens.gu.edu.au/robertk/R/devel/00b/0024.html>

    Kjetil> After evaluating this code, what you would do to use it is:
    Kjetil> hist(x)
    Kjetil> lines(hist(y, plot=FALSE))

Kjetil, did I forget to tell you that your above "hack" has made it 
(after a bit of modification) into
R 1.2 [due on Dec 15] ?

Here is the NEWS entry (under "NEW FEATURES")

    o	hist() now returns a "histogram" object and calls the new
	function plot.histogram() for plotting.
	It now also allows character labels.

--

Thanks for your contribution!

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
(perhaps better for general discussion)

par(new=TRUE) 

Just a casual aside - why has the standard been new=TRUE for overplotting
on old graph?  This has always seemed perverse and counter-intuitive to me
but perhaps there is an explanation - other than for compatability with S+
of course.  Anyone know?

John

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
j.logsdon at lancaster.ac.uk writes:
The logic is as follows: When something is plotted to a frame, the
paper is dirtied, so to speak, by saying "this is not a new frame",
which is reflected in par(new) == FALSE. Conversely, when advancing to
a new frame, we use plot.new() which sets par(new) == TRUE. High level
plots query par(new) to see whether it is necessary to advance to a
new frame. So explicitly setting par(new)<-TRUE means "pretend this is
a new frame, even though someone already scribbled on it".

I get it wrong half the time too, though... We have frame() as a
more intuitive version of plot.new(), maybe one could think of an
alias for par(new=TRUE) too? overplot() perhaps?