R软件中的hist()函数
(2011-10-26 11:19:49)
标签:
杂谈 |
分类: R软件学习 |
| hist {graphics} | R Documentation |
Histograms
Description
The generic
function hist plot=TRUE, the resulting object
of "histogram" plot.histogram,
before it is returned.
Usage
hist(x, ...)
## Default S3 method:
hist(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, warn.unused = TRUE, ...)
Arguments
x |
a vector of values for which the histogram is desired. |
breaks |
one of:
|
freq |
logical;
if TRUE, the histogram graphic is a
representation of frequencies,
thecounts FALSE, probability densities,
componentdensity, are plotted (so that the histogram
has a total area of one). Defaults
to TRUE breaks probability |
probability |
an !freq,
for S compatibility. |
include.lowest |
logical;
if TRUE,
an x[i] breaks right = FALSE) bar. This will be
ignored (with a warning)
unless breaks |
right |
logical;
if TRUE, the histogram cells are
right-closed (left open) intervals. |
density |
the density
of shading lines, in lines per inch. The default value
of NULL density |
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 NULL |
border |
the color of the border around the bars. The default is to use the standard foreground color. |
main,
xlab, ylab |
these
arguments
to title |
xlim,
ylim |
the range of
x and y values with sensible defaults. Note
that xlim plot = TRUE). |
axes |
logical.
If TRUE |
plot |
logical.
If TRUE plot =
TRUE |
labels |
logical or
character. Additionally draw labels on top of bars, if
not FALSE; seeplot.histogram. |
nclass |
numeric
(integer). For S(-PLUS) compatibility
only, nclass breaks |
warn.unused |
logical.
If plot=FALSE warn.unused=TRUE,
a warning will be issued when graphical parameters are passed
to hist.default(). |
... |
further
arguments and plot.histogram title axis plot=TRUE). |
Details
The definition
of breaks. Thus the
height of a rectangle is proportional to the number of points
falling into the cell, as is the
areaprovided
The default with non-equi-spaced breaks is to give a plot of
area one, in which
the
If right =
TRUE (a, b], i.e.,
they include their right-hand endpoint, but not their left one,
with the exception of the first cell
wheninclude.lowest TRUE.
For right = FALSE, the intervals
are of the form [a, b),
and include.lowest
A numerical tolerance
of breaks density.
The default
for breaks "Sturges":
see nclass.Sturges.
Other names for which algorithms are supplied
are "Scott" "FD" "Freedman-Diaconis" nclass.scott nclass.FD).
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"
breaks |
the breaks |
counts |
n x[] |
density |
values all(diff(breaks) == 1),
they are the relative
frequencies counts/n breaks[i]. |
intensities |
same
as density. Deprecated, but retained
for compatibility. |
mids |
the |
xname |
a character
string with the
actual x |
equidist |
logical,
indicating if the distances
between breaks |
References
Becker, R. A., Chambers, J. M. and Wilks, A. R.
(1988)
Venables, W. N. and Ripley. B. D.
(2002)
See Also
nclass.Sturges, stem, density, truehist
Typical plots with vertical bars
are barplot plot(*,
type = "h")for such bar plots.
Examples
op <- par(mfrow=c(2, 2))
hist(islands)
utils::str(hist(islands, col="gray", labels = TRUE))
hist(sqrt(islands), breaks = 12, col="lightblue", border="pink")
##-- For non-equidistant breaks, counts should NOT be graphed unscaled:
r <- hist(sqrt(islands), breaks = 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)
require(utils) # for str
str(hist(islands, breaks=12, plot= FALSE)) #-> 10 (~= 12) breaks
str(hist(islands, breaks=c(12,20,36,80,200,1000,17000), plot = FALSE))
hist(islands, breaks=c(12,20,36,80,200,1000,17000), freq = TRUE,
main = "WRONG histogram") # and warning
require(stats)
set.seed(14)
x <- rchisq(100, df = 4)
## Comparing data with a model distribution should be done with qqplot()!
qqplot(x, qchisq(ppoints(x), df = 4)); abline(0,1, col = 2, lty = 2)
## if you really insist on using hist() ... :
hist(x, freq = FALSE, ylim = c(0, 0.2))
curve(dchisq(x, df = 4), col = 2, lty = 2, lwd = 2, add = TRUE)

加载中…