[R] Histograms and Sturges rule

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Nov 28 01:32:10 CET 2007


On 11/27/07, Paul Smith <phhs80 at gmail.com> wrote:
> Dear All,
>
> According to the Sturges rule, the number of classes of a histogram is
> the closest integer to
>
> 1 + logb(n,base=2)
>
> where n is the number of observations. The function hist(), by
> default, uses the Sturges rule. However, the code
>
> x <- 1:200
> hist(x)
>
> produces a histogram with 10 classes and not 9 classes as determined
> by the Sturges rule. What am I missing?

The documentation does say that:

 In the last three cases the number is a suggestion only.

Basically, hist() uses "pretty" breakpoints, and you have

> pretty(c(1, 200),  9)
 [1]   0  20  40  60  80 100 120 140 160 180 200

(i.e. 10 intervals even when you ask for 9). truehist() in MASS and
histogram() in lattice don't have this "feature" (though truehist
doesn't seem to allow "Sturges" as an option).

-Deepayan



More information about the R-help mailing list