[R] ggplot2 histograms
Small Sandy (NHS Greater Glasgow & Clyde)
sandy.small at nhs.net
Wed Dec 1 12:09:55 CET 2010
Thanks Thierry
Using the position_dodge positioning option ought to work but there is something wrong with the scaling when the binwidth is not = 1.
You can begin to see this with the example I sent:
If you do
ggplot(data=dafr, aes(x = d1, fill=d2)) + geom_histogram(binwidth = 1, position = position_dodge())
You get the interleaved histogram you would expect
Similarly if you do:
ggplot(data=dafr, aes(x = d1, fill=d2)) + geom_histogram(binwidth = 2, position = position_dodge())
The histogram produced is reasonable: however
However if you do:
ggplot(data=dafr, aes(x = d1, fill=d2)) + geom_histogram(binwidth = 1, position = position_dodge(width=0.99))
The position of first bin which goes from 0-2 appears to start at about 0.2 (I accept that there is some "white space" to the left of this) while the position of the last bin (16-18) appears to start at about 15.8, so the whole histogram seems to be wrongly compressed into the scale. In my real data which has potentially 250 bins the problem becomes much more pronounced. Has any one else noticed this? Is there a work around?
I could just use a frquency polygon as suffested by Hadley but on a 6 x 3 facet grid solid blocks are considerably more eye catching.
Thanks for any help
Sandy
Sandy Small
Clinical Physicist
NHS Forth Valley
(Tel: 01324567002)
and
NHS Greater Glasgow and Clyde
(Tel: 01412114592)
________________________________________
From: ONKELINX, Thierry [Thierry.ONKELINX at inbo.be]
Sent: 30 November 2010 14:57
To: Small Sandy (NHS Greater Glasgow & Clyde); r-help at r-project.org
Subject: RE: [R] ggplot2 histograms
Dear Sandy,
Have a look at the position argument of geom_histogram.
ggplot(data=dafr, aes(x = d1, fill=d2)) + geom_histogram(binwidth = 1,
position = position_dodge())
ggplot(data=dafr, aes(x = d1, fill=d2)) + geom_histogram(binwidth = 1,
position = position_dodge(width = 0.5), alpha = 0.5)
Best regards,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] Namens Small Sandy (NHS
> Greater Glasgow & Clyde)
> Verzonden: dinsdag 30 november 2010 15:37
> Aan: r-help at r-project.org
> Onderwerp: [R] ggplot2 histograms
>
> 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,1
6,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.
>
********************************************************************************************************************
This message may contain confidential information. If you are not the intended recipient please inform the
sender that you have received the message in error before deleting it.
Please do not disclose, copy or distribute information in this e-mail or take any action in reliance on its contents:
to do so is strictly prohibited and may be unlawful.
Thank you for your co-operation.
NHSmail is the secure email and directory service available for all NHS staff in England and Scotland
NHSmail is approved for exchanging patient data and other sensitive information with NHSmail and GSi recipients
NHSmail provides an email address for your career in the NHS and can be accessed anywhere
For more information and to find out how you can switch, visit www.connectingforhealth.nhs.uk/nhsmail
More information about the R-help
mailing list