hist2d: Compute a 2D histogram

View source: R/hist2d.R

hist2dR Documentation

Compute a 2D histogram

Description

Compute a two-dimesnional histogram from two vectors (of the same length), x and y.

Usage

hist2d(x, y, nb = c(100, 100), xlim = range(x), ylim = range(y))

Arguments

x

a numeric vector.

y

a numeric vector of the same length as x.

nb

a vector c(nbx, nby), or a scalar nb, providing the number of bins for x, and y; default is 100; see details.

xlim

a vector c(xmin, xmax) containing the limits of the domain of x; default range(x).

ylim

a vector c(ymin, ymax) containing the limits of the domain of y; default range(y).

Details

If nb is scalar, it is extended to c(nb, nb), so that both dimensions will have the same number of bins.

Elements of x (y) that fall outside the range specified by xlim (ylim) are not counted.

Value

A list with components:

H

a matrix of dimension nbx by nby containing bin counts.

xgrid

a vector of length nbx representing centers of the bins for x.

ygrid

a vector of length nby representing centers of the bins for y.

xbin

a vector giving the bin number of each element of x.

ybin

a vector giving the bin number of each element of y.

References

Eilers, P.H.C. and Marx, B.D. (2021). Practical Smoothing, The Joys of P-splines. Cambridge University Press.

Examples

data(faithful)
x = faithful$eruptions
y = faithful$waiting
C = hist2d(x, y, c(50,50))
image(C$xgrid, C$ygrid, C$H, xlab='Eruption length (min)', ylab='Waiting time (min)')
title('Old Faithful geyser')


JOPS documentation built on Sept. 8, 2023, 5:42 p.m.

Related to hist2d in JOPS...