car (version 1.2-9)

n.bins: Number of Bins for Histogram

Description

Several rules for calculating the number of bins to use for a histogram.

Usage

n.bins(x, rule=c("freedman.diaconis", "sturges", "scott", "simple"))

Arguments

x
numeric vector, variable for histogram
rule
see below.

Value

  • the number of bins.

Details

[object Object],[object Object],[object Object],[object Object] where $n$ is the number of observations, $range$ is the range of x, $IQR$ is the inter-quartile range of x, and $s$ is the standard deviation of x.

References

Freedman, D. and Diaconis, P. (1981) On the histogram as a density estimator. Zeitschrift fur Wahrscheinlichkeitstheorie und verwandte Gebiete 57, 453--476. Scott, D. W. (1979) On optimal and data based-histograms. Biometrika 66, 605--610. Venables, W. N. and Ripley, B. D. (1999) Modern Applied Statistics with S-PLUS, Third Edition, Springer.

See Also

hist

Examples

Run this code
attach(UN)
n.bins(gdp)
## [1] 19
n.bins(gdp, "sturges")
## [1] 9
n.bins(gdp, "scott")
## [1] 8
n.bins(gdp, "simple")
## [1] 22
hist(gdp)
hist(gdp, nclass=n.bins(gdp))

Run the code above in your browser using DataCamp Workspace