# Re: [R] superimposing histograms con't [Broadcast]

From: Wiener, Matthew <matthew_wiener_at_merck.com>
Date: Thu 29 Jun 2006 - 06:51:35 EST

I wrote some code to do this. It only works with 2 groups (that's all I needed), but could probably be generalized. It got my graph made, and I haven't needed a graph like this one again, so I never went back to really clean it up.

It works by first plotting both sets of rectangles, then going back over the ones that had the first bar totally covered by the second.

Hope this helps,

Matt Wiener

"f.back.front.hist" <-
function(breaks, x1, x2, col1 = "gray50", col2 = "white",

```           lwd = diff(range(mids))/20,
xlab = "", ylab = "",
leg.text = NULL, ...){
if(length(x1) != length(x2))
stop("x1 and x2 must have same length")
if(length(x1) != length(breaks) - 1)
stop("length of breaks must be 1 more than length of x1 and x2")
```
plot.lim <- c(0, max(c(x1, x2)))
mids <- 0.5 * (breaks[-1] + breaks[-length(breaks)])     plot(mids, pmax(x1, x2), col = par()\$bg, lwd = lwd,
```         ylim = plot.lim, xlab = xlab, ylab = ylab,
cex.axis = 1.5, font.axis = 2, cex.lab = 1.5, font.lab = 2, ...)
```
rect(breaks[-length(breaks)], 0, breaks[-1], x1, col = col1)     rect(breaks[-length(breaks)], 0, breaks[-1], x2, col = col2)     ind <- x1 < x2
rect(breaks[-length(breaks)][ind], 0,

breaks[-1][ind], x1[ind] ,col = col1)     if(!is.null(leg.text))

```      legend(mids, plot.lim, leg.text,
fill = c(col1, col2), cex = 1.5)

```

}

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Jeff Miller
Sent: Wednesday, June 28, 2006 4:21 PM
To: 'Bill Shipley'; 'R help list'
Subject: Re: [R] superimposing histograms con't [Broadcast]

I would like to do the same but WITH overlapping.

For example, I graph 2 sets of count data. Say the bars for the 1`s overlap...I would like to show that with a different shading for the group that has the higher frequency. For example, it could be black up to a frequency of 5 followed by diagonal-dashes from 5-7 representing the higher frequency of a second group.

Thank you,
Jeff Miller

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Bill Shipley
Sent: Wednesday, June 28, 2006 3:54 PM
To: R help list
Subject: [R] superimposing histograms con't

Earlier, I posted the following question: I want to superimpose histograms from three populations onto the same graph, changing the shading of the bars for each population. After consulting the help files and the archives I cannot find out how to do this (seemly) simple graph. To be clear, I want
- a single x axis (from -3 to 18)

• three groups of bars forming the histograms of each population (they will not overlap much, but this is a detail) - the bars from each histogram having different shadings or other visually distinguishing features.

Gabor Grothendieck [ggrothendieck@gmail.com] pointed to some code to to this but I have found another way that works even easier.

par(new=T) - to keep on the same graph

hist(x[sel2],main=Null,xlab=NULL,ylab=NULL,axes=F) -superimposes the second histogram

par(new=T) - to keep on the same graph

hist(x[sel3],main=Null,xlab=NULL,ylab=NULL,axes=F) -superimposes the third histogram

Bill Shipley

North American Editor, Annals of Botany

Editor, "Population and Community Biology" series, Springer Publishing

Dipartement de biologie, Universiti de Sherbrooke,

Bill.Shipley@USherbrooke.ca

[[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list