[R] A question on histogram - area much less than 1

David L Carlson dcarlson at tamu.edu
Mon Mar 12 17:07:33 CET 2012


You expected the sum to be 1 and it is:

> a <- hist(Y1, breaks=seq(0, 350, by=35), col="grey80", freq=FALSE)
> a$density*35
 [1] 0.27923387 0.14717742 0.09979839 0.08064516 0.06955645 0.06350806
 [7] 0.05241935 0.05342742 0.05544355 0.09879032
> sum(a$density*35)
[1] 1

Note that the first density multiplied by 35 is .279 exactly what you
expected and the sum of the densities multiplied by the width of each bar
(35) is 1. The height of the bar is not the probability, the area of the bar
is the probability.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Rolf Turner
> Sent: Monday, March 12, 2012 3:53 AM
> To: jpm miao
> Cc: r-help at r-project.org
> Subject: Re: [R] A question on histogram - area much less than 1
> 
> On 12/03/12 21:44, jpm miao wrote:
> > Hello,
> >
> >    Thanks very much for your kind response. Yes, if I multiply by the
> > width "35", the area should be equal to one.
> >
> >    How can I plot the probability bars rather than density bars? That
> > is, I would like the height of the first bar to be 0.279, which is
> the
> > probability that the variable falls between 0 and 35.
> 
> If you want probabilities rather than densities you should be using
> barplot() rather
> than histogram:
> 
> TBL <- table(Y1)
> barplot(TBL/sum(TBL))
> 
>      cheers,
> 
>          Rolf Turner
> 
> ______________________________________________
> 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.



More information about the R-help mailing list