Produces weighted histograms by adding a "weight" option to the his.default function from the graphics package (Copyright R-core). The code here was copied from that function and modified slightly to allow for weighted histograms as well as unweighted histograms. 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.
wtd.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, weight = NULL, ...)
a vector of values for which the histogram is desired.
one of:
a vector giving the breakpoints between histogram cells,
a single number giving the number of cells for the histogram,
a character string naming an algorithm to compute the number of cells (see ‘Details’),
a function to compute the number of cells.
In the last three cases the number is a suggestion only.
logical; if TRUE
, the histogram graphic is a
representation of frequencies, the counts
component of
the result; if FALSE
, probability densities, component
density
, are plotted (so that the histogram has a total area
of one). Defaults to TRUE
if and only if breaks
are
equidistant (and probability
is not specified).
an alias for !freq
, for S compatibility.
logical; if TRUE
, an x[i]
equal to
the breaks
value will be included in the first (or last, for
right = FALSE
) bar. This will be ignored (with a warning)
unless breaks
is a vector.
logical; if TRUE
, the histogram cells are
right-closed (left open) intervals.
the density of shading lines, in lines per inch.
The default value of NULL
means that no shading lines
are drawn. Non-positive values of density
also inhibit the
drawing of shading lines.
the slope of shading lines, given as an angle in degrees (counter-clockwise).
a colour to be used to fill the bars.
The default of NULL
yields unfilled bars.
the color of the border around the bars. The default is to use the standard foreground color.
these arguments to title
have useful
defaults here.
the range of x and y values with sensible defaults.
Note that xlim
is not used to define the histogram (breaks),
but only for plotting (when plot = TRUE
).
logical. If TRUE
(default), axes are draw if the
plot is drawn.
logical. If TRUE
(default), a histogram is
plotted, otherwise a list of breaks and counts is returned. In the
latter case, a warning is used if (typically graphical) arguments
are specified that only apply to the plot = TRUE
case.
logical or character. Additionally draw labels on top
of bars, if not FALSE
; see plot.histogram
in the graphics
package.
numeric (integer). For S(-PLUS) compatibility only,
nclass
is equivalent to breaks
for a scalar or
character argument.
numeric. Defines a set of weights to produce a weighted histogram. Will default to 1 for each case if no other weight is defined.
further arguments and graphical parameters passed to
plot.histogram
and thence to title
and
axis
(if plot=TRUE
).
an object of class "histogram"
which is a list with components:
the \(n+1\) cell boundaries (= breaks
if that
was a vector). These are the nominal breaks, not with the boundary fuzz.
\(n\) values; for each cell, the number of
x[]
inside.
values for each bin such that the area under the histogram totals 1.
\(\hat f(x_i \omega_i)\) / \(f^(x[i] \omega[i])\), as estimated
density values. If all(diff(breaks) == 1)
, they are the
relative frequencies counts/n
and in general satisfy
\(\sum_i \hat f(x_i \omega_i) (b_{i+1}-b_i) = 1\) / \(sum[i; f^(x[i] \omega[i])
(b[i+1]-b[i])] = 1\), where \(b_i\) = breaks[i]
.
same as density
. Deprecated, but retained
for compatibility.
the \(n\) cell midpoints.
a character string with the actual x
argument name.
logical, indicating if the distances between
breaks
are all the same.
The definition of histogram differs by source (with
country-specific biases). R's default with equi-spaced breaks (also
the default) is to plot the (weighted) counts in the cells defined by
breaks
. Thus the height of a rectangle is proportional to
the (weighted) number of points falling into the cell, as is the area
provided the breaks are equally-spaced.
The default with non-equi-spaced breaks is to give a plot of area one, in which the area of the rectangles is the fraction of the data points falling in the cells.
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
means ‘include highest’.
The default for breaks
is "Sturges"
: see
nclass.Sturges
. Other names for which algorithms
are supplied are "Scott"
and "FD"
/
"Freedman-Diaconis"
(with corresponding functions
nclass.scott
and 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
.
# NOT RUN {
var1 <- c(1:100)
wgt <- var1/mean(var1)
par(mfrow=c(2, 2))
wtd.hist(var1)
wtd.hist(var1, weight=wgt)
wtd.hist(var1, weight=var1)
# }
Run the code above in your browser using DataLab