hexbin {hexbin}R Documentation

bivariate binning into hexagon cells


Creates a "hexbin" object. Basic components are a cell id and a count of points falling in each occupied cell.

Basic methods are plot() (plot.hexbin) and summary() (summary.hexbin), but also the functions smooth.hexbin and erode.hexbin.


hexbin(x, y, xbins=30, shape=1, xbnds=range(x), ybnds=range(y), IDs = FALSE)


x a vector of numbers. The first coordinate of bivariate data to be binned.
y a vector of numbers. The second coordinate of bivariate data to be binned.
xbins the number of bins partitioning the range of xbnds.
shape the shape = yheight/xwidth of the plotting regions.
xbnds, ybnds horizontal and vertical limits of the binning region in x or y units respectively; must be numeric vector of length 2.
IDs logical indicating if the individual cell “IDs” should be returned, see also below.


Returns counts for non-empty cells. The plot shape must be maintained for hexagons to appear with equal sides. Some calculations are in single precision.

Note that when plotting with plot.hexbin, the plotting parameters are changed during the plot and it returns the changed par parameters, so to utilize the same plot window one must be sure to also use these par values for it to work properly (see the use of screenpar in the examples below).


an object of class "hexbin". The list structure typically contains the following components:

cell vector of cell ids that can be mapped into the (x,y) bin centers in data units.
cnt vector of counts in the cells.
xcm The x center of mass (average of x values) for the cell.
ycm The y center of mass (average of y values) for the cell.
xbins number of hexagons across the x axis. hexagon inner diameter =diff(xbnds)/xbins in x units
xbnds x coordinate bounds for binning and plotting
ybnds y coordinate bounds for binning and plotting
shape plot shape which is yheight(inches) / xwidth(inches)
dim The i and j limits of cnt treated as a matrix cnt[i,j]
cell.id only if IDs was true, an integer vector of length n where cell.id[i] is the cell number of the i-th original point (x[i], y[i]).


Carr, D. B. et al. (1987) Scatterplot Matrix Techniques for Large N. JASA 83, 398, 424–436.

See Also

hcell2xy, hcell, plot.hexbin, hboxplot, hdiffplot, hmatplot, hexagons, hex.legend.


x <- rnorm(10000)
y <- rnorm(10000)
bin <- hexbin(x,y)

## Using plot method for hexbin objects:
plot(bin, style = "nested.lattice")

## lower resolution binning and overplotting with counts
bin <- hexbin(x,y,xbins=25)
screenpar <- plot.hexbin(bin, style="lattice", minarea=1, maxarea=1,
                   density=0, border=TRUE)
oldpar <- par(screenpar)     # reset graphics to the plot on the screen
xy <- hcell2xy(bin)
points(x,y)       # to show points rather than counts
par(oldpar)       #reset graphics

## Use (and 'check') IDs:
bin3 <- hexbin(x, y, IDs = TRUE)
tb <- table(bin3$cell.id)
stopifnot(bin3$cnt  == c(tb),
          bin3$cell == rownames(tb))

[Package hexbin version 1.0.10 Index]