[R] Combining two histograms

Deepayan Sarkar deepayan at stat.wisc.edu
Wed Feb 2 16:31:24 CET 2005


On Wednesday 02 February 2005 07:42, Sean Davis wrote:
> 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)

This should be ~a | b. That 'a | b' works is undocumented, unintutive 
and liable to change.

> 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.

This is my personal bias to some extent, but I would strongly suggest 
you use densityplot instead, e.g. 

densityplot(~a, groups = b, plot.points = FALSE) 

or if you are suspicious of more sophisticated kernels, 

densityplot(~a, groups = b, plot.points = FALSE, kern = "rect")

Histograms were appropriate for drawing density estimates by hand in the 
good old days, but I can imagine very few situations where I would not 
prefer to use smoother density estimates when I have the computational 
power to do so.

Deepayan




More information about the R-help mailing list