Histograms and Kernel Density Plots
Description
Draw Histograms and Kernel Density Plots, possibly conditioned on other variables.
Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58  histogram(x, data, ...)
densityplot(x, data, ...)
## S3 method for class 'formula'
histogram(x,
data,
allow.multiple, outer = TRUE,
auto.key = FALSE,
aspect = "fill",
panel = lattice.getOption("panel.histogram"),
prepanel, scales, strip, groups,
xlab, xlim, ylab, ylim,
type = c("percent", "count", "density"),
nint = if (is.factor(x)) nlevels(x)
else round(log2(length(x)) + 1),
endpoints = extend.limits(range(as.numeric(x),
finite = TRUE), prop = 0.04),
breaks,
equal.widths = TRUE,
drop.unused.levels =
lattice.getOption("drop.unused.levels"),
...,
lattice.options = NULL,
default.scales = list(),
default.prepanel =
lattice.getOption("prepanel.default.histogram"),
subscripts,
subset)
## S3 method for class 'numeric'
histogram(x, data = NULL, xlab, ...)
## S3 method for class 'factor'
histogram(x, data = NULL, xlab, ...)
## S3 method for class 'formula'
densityplot(x,
data,
allow.multiple = is.null(groups)  outer,
outer = !is.null(groups),
auto.key = FALSE,
aspect = "fill",
panel = lattice.getOption("panel.densityplot"),
prepanel, scales, strip, groups, weights,
xlab, xlim, ylab, ylim,
bw, adjust, kernel, window, width, give.Rkern,
n = 50, from, to, cut, na.rm,
drop.unused.levels =
lattice.getOption("drop.unused.levels"),
...,
lattice.options = NULL,
default.scales = list(),
default.prepanel =
lattice.getOption("prepanel.default.densityplot"),
subscripts,
subset)
## S3 method for class 'numeric'
densityplot(x, data = NULL, xlab, ...)
do.breaks(endpoints, nint)

Arguments
x 
The object on which method dispatch is carried out. For the As a special case, the right hand side of the formula can contain
more than one term separated by ‘+’ signs (e.g., For the 
data 
For the 
type 
A character string indicating the type of histogram that is to be
drawn.

nint 
An integer specifying the number of histogram bins, applicable only
when 
endpoints 
A numeric vector of length 2 indicating the range of xvalues that
is to be covered by the histogram. This applies only when

breaks 
Usually a numeric vector of length (number of bins + 1) defining the
breakpoints of the bins. Note that when breakpoints are not equally
spaced, the only value of When breaks = seq_len(1 + nlevels(x))  0.5 when breaks = do.breaks(endpoints, nint) otherwise. Breakpoints calculated in such a manner are used in all
panels. If the retrieved value is not When specified explicitly, a special value of 
equal.widths 
A logical flag, relevant only when 
n 
Integer, giving the number of points at which the kernel density is
to be evaluated. Passed on as an argument to 
panel 
A function, called once for each panel, that uses the packet (subset
of panel variables) corresponding to the panel to create a display.
The default panel functions 
allow.multiple, outer 
See 
auto.key 
See 
aspect 
See 
prepanel 
See 
scales 
See 
strip 
See 
groups 
See 
xlab, ylab 
See 
xlim, ylim 
See 
drop.unused.levels 
See 
lattice.options 
See 
default.scales 
See 
subscripts 
See 
subset 
See 
default.prepanel 
Fallback prepanel function. See 
weights 
numeric vector of weights for the density
calculations, evaluated in the nonstandard manner used for
At the time of writing, 
bw, adjust, width 
Arguments controlling bandwidth. Passed on as arguments to

kernel, window 
The choice of kernel. Passed on as arguments to

give.Rkern 
Logical flag, passed on as argument to 
from, to, cut 
Controls range over which density is evaluated. Passed on as
arguments to 
na.rm 
Logical flag specifying whether 
... 
Further arguments. See corresponding entry in

Details
histogram
draws Conditional Histograms, and densityplot
draws Conditional Kernel Density Plots. The default panel function
uses the density
function to compute the density
estimate, and all arguments accepted by density
can be
specified in the call to densityplot
to control the output.
See documentation of density
for details. Note that the
default value of the argument n
of density
is changed to
50.
These and all other high level Trellis functions have several
arguments in common. These are extensively documented only in the
help page for xyplot
, which should be consulted to learn more
detailed usage.
do.breaks
is an utility function that calculates breakpoints
given an interval and the number of pieces to break it into.
Value
An object of class "trellis"
. The
update
method can be used to
update components of the object and the
print
method (usually called by
default) will plot it on an appropriate plotting device.
Note
The form of the arguments accepted by the default panel function
panel.histogram
is different from that in SPLUS. Whereas
SPLUS calculates the heights inside histogram
and passes only
the breakpoints and the heights to the panel function, lattice
simply passes along the original variable x
along with the
breakpoints. This approach is more flexible; see the example below
with an estimated density superimposed over the histogram.
Author(s)
Deepayan Sarkar Deepayan.Sarkar@Rproject.org
References
Sarkar, Deepayan (2008) Lattice: Multivariate Data Visualization with R, Springer. http://lmdvr.rforge.rproject.org/
See Also
xyplot
,
panel.histogram
,
density
,
panel.densityplot
,
panel.mathdensity
,
Lattice
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  require(stats)
histogram( ~ height  voice.part, data = singer, nint = 17,
endpoints = c(59.5, 76.5), layout = c(2,4), aspect = 1,
xlab = "Height (inches)")
histogram( ~ height  voice.part, data = singer,
xlab = "Height (inches)", type = "density",
panel = function(x, ...) {
panel.histogram(x, ...)
panel.mathdensity(dmath = dnorm, col = "black",
args = list(mean=mean(x),sd=sd(x)))
} )
densityplot( ~ height  voice.part, data = singer, layout = c(2, 4),
xlab = "Height (inches)", bw = 5)

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker. Vote for new features on Trello.