| hist {base} | R Documentation |
Histograms
Description
The generic function hist computes a histogram of the given
data values. If plot=TRUE, the resulting object of
class "histogram" is plotted by
plot.histogram, before it is returned.
Usage
hist(x, ...)
hist.default(x, breaks = "Sturges", freq = NULL, probability = !freq,
include.lowest = TRUE, right = TRUE,
density = NULL, angle = 45, col = NULL, border = NULL,
main = paste("Histogram of" , xname),
xlim = range(breaks), ylim = NULL,
xlab = xname, ylab,
axes = TRUE, plot = TRUE, labels = FALSE,
nclass = NULL, ...)
Arguments
x |
a vector of values for which the histogram is desired. |
breaks |
one of:
In the last three cases the number is a suggestion only. |
freq |
logical; if |
probability |
an alias for |
include.lowest |
logical; if |
right |
logical; if |
density |
the density of shading lines, in lines per inch.
The default value of |
angle |
the slope of shading lines, given as an angle in degrees (counter-clockwise). |
col |
a colour to be used to fill the bars.
The default of |
border |
the color of the border around the bars. The default is to use the standard foreground color. |
main, xlab, ylab |
these arguments to |
xlim, ylim |
the range of x and y values with sensible defaults. |
axes |
logical. If |
plot |
logical. If |
labels |
logical or character. Additionally draw labels on top
of bars, if not |
nclass |
numeric (integer). For S(-PLUS) compatibility only,
|
... |
further graphical parameters to |
Details
If right = TRUE (default), the histogram cells are intervals
of the form (a, b], i.e. they include their right-hand endpoint,
but not their left one, with the exception of the first cell when
include.lowest is TRUE.
For right = FALSE, the intervals are of the form [a, b),
and include.lowest really has the meaning of
“include highest”.
The default for breaks is "Sturges": see
nclass.Sturges. Other names for which algorithms
are supplied are "Scott" and "FD" /
"Friedman-Diaconis". Case is ignored and partial matching is used.
Alternatively, a function can be supplied which
will compute the intended number of breaks as a function of x.
Value
an object of class "histogram" which is a list with components:
breaks |
the |
counts |
|
density |
values |
intensities |
same as |
mids |
the |
xname |
a character string with the actual |
equidist |
logical, indicating if the distances between
|
Note
The resulting value does not depend on the values of
the arguments freq (or probability)
or plot. This is intentionally different from S.
References
Venables, W. N. and Ripley. B. D. (1999) Modern Applied Statistics with S-PLUS. Springer.
See Also
nclass.Sturges, stem,
density.
Examples
data(islands)
op <- par(mfrow=c(2, 2))
hist(islands)
str(hist(islands, col="gray", labels = TRUE))
hist(sqrt(islands), br = 12, col="lightblue", border="pink")
##-- For non-equidistant breaks, counts should NOT be graphed unscaled:
r <- hist(sqrt(islands), br = c(4*0:5, 10*3:5, 70, 100, 140), col='blue1')
text(r$mids, r$density, r$counts, adj=c(.5, -.5), col='blue3')
sapply(r[2:3], sum)
sum(r$density * diff(r$breaks)) # == 1
lines(r, lty = 3, border = "purple") # -> lines.histogram(*)
par(op)
str(hist(islands, plot= FALSE)) #-> 5 breaks
str(hist(islands, br=12, plot= FALSE)) #-> 10 (~= 12) breaks
str(hist(islands, br=c(12,20,36,80,200,1000,17000), plot = FALSE))
hist(islands, br=c(12,20,36,80,200,1000,17000), freq = TRUE,
main = "WRONG histogram") # and warning