[R] ggplot2 histograms

Hadley Wickham hadley at rice.edu
Tue Nov 30 21:09:59 CET 2010

You may find it easier to use a frequency polygon, geom = "freqpoly".


On Tue, Nov 30, 2010 at 2:36 PM, Small Sandy (NHS Greater Glasgow &
Clyde) <sandy.small at nhs.net> wrote:
> Hi
> With ggplot2 I can very easily create beautiful histograms but I would like to put two histograms on the same plot. The histograms may be over-lapping.
> When they are overlapping the bars are shown on top of each other (so that the overall height is the sum of the two). Is there any way to get them to display overlapping (with smaller value in front, larger value behind) so that the overall height is equal to the height of the largest value
> The following demonstrates the problem (there is probably a simple way to generate the sequence in d1 but I don't know it and just threw this together quickly)
> d1<-c(1,2,2,3,3,3,4,4,4,4,5,5,5,5,5,6,6,6,6,7,7,7,8,8,9,6,7,7,8,8,8,9,9,9,9,10,10,10,10,10,11,11,11,11,12,12,12,13,13,14,15,15,16,16,16,17,17,17,17,18,18,18,18,18)
> d2<-c(rep("a",25), rep("b",39))
> dafr<-data.frame(d1,d2)
> library(ggplot)
> qplot(d1, data=dafr, fill=d2, geom='histogram', binwidth = 1)
> Many thanks for any help
> Sandy
> Sandy Small
> Clinical Physicist
> NHS Forth Valley
> and
> NHS Greater Glasgow and Clyde
> ********************************************************************************************************************
> This message may contain confidential information. If yo...{{dropped:24}}
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University

More information about the R-help mailing list