geom-bin-2d

Two-dimensional bin layer: counts (x, y) into a rectangular grid and draws one rectangle per non-empty cell. The fill aesthetic defaults to _count, so a continuous fill scale (scale-fill-viridis-c, etc.) shades the cells by frequency.

bins and binwidth accept either a scalar or an (x, y) pair.

Usage

geom-bin-2d(
  mapping: none,
  data: none,
  bins: 30,
  binwidth: none,
  colour: auto,
  fill: auto,
  stroke: none,
  alpha: auto,
  inherit-aes: true,
)

Parameters

Parameter Default Description
mapping none Layer-specific aesthetic mapping built with aes. Must map x and y.
data none Layer-specific dataset. Falls back to the plot data when none.
bins 30 Scalar or (x, y) pair — target bin counts.
binwidth none Scalar or (x, y) pair — fixed bin widths. Overrides bins per axis.
colour auto Cell outline colour.
fill auto Cell fill colour. auto lets the fill scale paint by _count.
stroke none Outline thickness or stroke dictionary.
alpha auto Cell opacity in [0, 1].
inherit-aes true Whether to merge the plot-level mapping into this layer’s mapping.

Returns

Layer dictionary consumed by plot.

Examples

30-by-30 grid coloured by count via the magma palette.

#let d = range(0, 400).map(i => (
  x: calc.sin(i * 0.13) * 4,
  y: calc.cos(i * 0.21) * 4,
))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-bin-2d(bins: 20),),
  scales: (scale-fill-viridis-c(option: "magma"),),
  width: 10cm,
  height: 6cm,
)

Two-dimensional bin grid of sine/cosine samples with rectangular cells shaded by count via the magma palette.

Two-dimensional bin grid of sine/cosine samples with rectangular cells shaded by count via the magma palette.

See also

geom-hex, stat-bin-2d, geom-tile.

Back to top