[R] unexpected breaks in hist

Paul Murrell paul at stat.auckland.ac.nz
Mon May 7 04:43:21 CEST 2001


> I've been using hist to plot histograms of some data, but I get variable
> numbers of bins. I understood from reading the help file that breaks will
> set the number of bins. Please correct me if I'm wrong.
> I'm plotting measures from subsets of a larger data set. Depending on the
> data subset the number of bins varies despite having set breaks to 5 in
> all cases.

The crucial part of the help is the word "approximate" ...

  breaks: either a single number giving the approximate number of cells
          for the histogram or a vector giving the breakpoints between
          histogram cells.

... R takes your suggestion for the number of bins and modifies it if
necessary to produce a "nice" number of bins.

You can have exact control over the number of bins if you give the bin
breaks.  For example, compare ...

    for (i in 1:4)
        hist(rnorm(50), breaks=4)  # sometimes 6 bins, sometimes 5 bins, ...

... with ...

    for (i in 1:4)
        hist(rnorm(50), breaks=c(-4,-2,0,2,4)) # always 4 bins

Hope that helps


r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list