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
Sean If you want two bars for each bin then that sounds more like barplot() could be used. I know you can tightly control the distance between bars and the width of the bars, and so with a little coding you could probably use that. Mick -----Original Message----- From: r-help-bounces at stat.math.ethz.ch on behalf of Sean Davis Sent: Wed 2/2/2005 1:42 PM To: r-help Cc: 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
Something a bit more sohpisticated than this: a <- rnorm(20000) b <- rnorm(20000, mean=1.5) ah <- hist(a,breaks= seq(-5,6,by=0.2),plot=FALSE) bh <- hist(b,breaks= seq(-5,6,by=0.2),plot=FALSE) data <- t(cbind(ah$counts,bh$counts)) barplot(data,beside=TRUE, space=rep(0,2*ncol(data))) -----Original Message----- From: r-help-bounces at stat.math.ethz.ch on behalf of Sean Davis Sent: Wed 2/2/2005 1:42 PM To: r-help Cc: 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
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 > >
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