[R] Combining two histograms

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Feb 3 03:08:38 CET 2005


Looking at the output from this and the previous response using barplot
worries me for two reasons :

a) The bars in "front" may obscure the bars at the "back". The order of
plotting becomes important here.

b) IMHO, it is distracting and does not convey information well unless
one is willing to risk a headache looking closer at the plots.



Personally, I prefer looking at density curves especially if I want to
compare many variables.

# simulate data
x1 <- rnorm(1000, 0.4, 0.8)
x2 <- rnorm(1000, 0.0, 1.0)
x3 <- rnorm(1000, -1.0, 1.0)

# density plots
plot( density(x1), xlim=range( c(x1, x2, x3) ), main="", xlab="" )
lines(density(x2), col=2)
lines(density(x3), col=3)

# rug plots for displaying actual data points
# you can add jitter() to rug() but with 1000 obs not much difference
rug(x1, col=1, ticksize=0.01, line=2.5)
rug(x2, col=2, ticksize=0.01, line=3.0)
rug(x3, col=3, ticksize=0.01, line=3.5)


Regards, Adai


On Wed, 2005-02-02 at 16:27 +0100, Henrik Bengtsson wrote:
> If you looking for something like 
> 
> x1 <- rnorm(1000,  0.4, 0.8)
> x2 <- rnorm(1000,  0.0, 1.0)
> x3 <- rnorm(1000, -1.0, 1.0)
> hist(x1, width=0.33, offset=0.00, col="blue", xlim=c(-4,4),
>  main="Histogram of x1, x2 & x3", xlab="x1 - blue, x2 - red, x3 - green")
> hist(x2, width=0.33, offset=0.33, col="red", add=TRUE)
> hist(x3, width=0.33, offset=0.66, col="green", add=TRUE)
> 
> with results as in http://www.maths.lth.se/help/R/plot.histogram/ there is
> an extension to hist() (actually plot.histogram()) in the R.basic package
> (part of the R.classes bundle). See
> http://www.maths.lth.se/help/R/R.classes/ for installation instructions.
> 
> BTW, you should also consider plotting density() estimates.
> 
> Henrik Bengtsson
> 
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch 
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Sean Davis
> > Sent: Wednesday, February 02, 2005 2:42 PM
> > To: r-help
> > Subject: [R] Combining two histograms
> > 
> > 
> > I have data like:
> > 
> > a <- rnorm(20000)
> > b <- rep(FALSE,20000)
> > b[sample(1:20000,15000)] <- TRUE
> > 
> > Using Lattice graphics, I can produce two side-by-side 
> > histograms quite 
> > easily by:
> > 
> > histogram(a | b)
> > 
> > However, I would like to produce a "single" histogram with two bars 
> > within each bin, one for each group, as the groups are in 
> > reality very 
> > slightly different from each other.  The difference isn't evident 
> > unless one "overlays" the two histograms in some manner.
> > 
> > Thanks,
> > Sean
> > 
> > ______________________________________________
> > R-help at 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
> > 
> >
> 
> ______________________________________________
> R-help at 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
>




More information about the R-help mailing list