Calculate Two-Dimensional Histogram

DESCRIPTION:

Returns a structure for a 2-dimensional histogram which can be given to a graphics function such as image or persp. The user may supply an explicit list of breakpoints, or a suggested number of classes.

USAGE:

hist2d(x, y, xbreaks=<<see below>>, ybreaks=<<see below>>,  
        nxbins=<<see below>>, nybins=<<see below>>, scale=F) 

REQUIRED ARGUMENTS:

x,y
vectors representing the two dimensions of the data. x can give both dimensions by being a list with x and y components, or a matrix with two columns (the first column is x, the second column is y). Missing values are not allowed.

OPTIONAL ARGUMENTS:

xbreaks
breakpoints for the bins on the x coordinate of the histogram.
ybreaks
breakpoints for the bins on the y coordinate of the histogram.
nxbins
approximate number of bins into which to place the x data, nxbin is ignored if xbreaks is supplied.
nybins
approximate number of bins into which to place the y data, nybin is ignored if ybreaks is supplied.
scale
if TRUE, z is scaled so that the volume under the histogram is 1. If FALSE, z contains the actual counts.

VALUE:

a list with the following components:
x
the centers of the x bins.
y
the centers of the y bins.
z
a matrix of bin counts. The number of rows is the length of the output x, and the number of columns is the length of the output y.
xbreaks
the boundaries used to bin x
ybreaks
the boundaries used to bin y

DETAILS:

Missing values are not counted. hist2d calculates the counts for a 2-dimensional histogram. These counts may be used in persp, image, contour or some other plotting routine. The default number of bins is computed by a variant of Doane's rule and breakpoints are chosen so that few datapoints lie on a breakpoint.

SEE ALSO:

, , .

EXAMPLES:

corn <- list(x = corn.rain, y = corn.yield) 
image(hist2d(corn, nxbins=9, ny=9)) 
points(corn, col=0) 
prim <- hist2d(prim9[,3:4], nxbin=30, nybin=30) 
persp(prim$z/max(prim$z), eye=c(-6, -4, 3))