[R] density in hist does not come as 1

Pascal Oettli kridox at ymail.com
Tue May 20 08:50:58 CEST 2014


Hello,

What you are doing wrong? Correctly read the help page.

>From the help page: "if FALSE, probability densities, component
density, are plotted (so that the histogram has a *total area of
one*)"

> sum(check$density * diff(check$breaks))
[1] 1

HTH,
Pascal

On Tue, May 20, 2014 at 3:06 PM, vikram ranga <babuawara at gmail.com> wrote:
> Dear List,
>
> I am trying to make a histogram with following data:
>
> dput(a)
> c(1, 0, 1.5, 1, 0, 0, 0, 1, 1.5, 0, 0, 1, 0, 0, 0, 1, 1, 2, 1,
> 0, 0, 0, 1, 1, 1, 0, 1, 0.5, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0,
> 0, 0, 0, 0, 1, 1, 1, 0, 0, 1.5, 0, 0, 0, 0, 1, 0, 0, 0.5, 0,
> 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 2, 0, 0, 0, 1, 1, 0,
> 0, 0, 1.5)
>
> So, I did
>
> hist(a, freq = FALSE, breaks = 5)
> but the first bar height is going above 1!
> I did some checking
>
> check <- hist(a, freq = FALSE, breaks = 5)
> check$density
>
> #[1] 1.146341 0.707317 0.097561 0.048780
>
> first bar went above 1, however, the values should be
> check$counts/length(a)
> #[1] 0.57317 0.35366 0.04878 0.02439
> sum(check$counts/length(a))
> #[1] 1
>
>
> What am I going wrong here?
>
> Thank you for your time!
>
> Regards,
>
> Vikram.
>
> sessionInfo()
>
> R version 3.1.0 (2014-04-10)
> Platform: i386-w64-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252
>  LC_MONETARY=English_India.1252
> [4] LC_NUMERIC=C                   LC_TIME=English_India.1252
> .
> .
> .
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.



-- 
Pascal Oettli
Project Scientist
JAMSTEC
Yokohama, Japan



More information about the R-help mailing list