stat-density-2d-filled

Filled 2D density statistic: iso-bands of a Gaussian kernel density estimate.

Smooths the (x, y) sample into a density surface on an n × n grid, then cuts the surface into level bands. Emits the same row shape as stat-contour-filled (closed polygons with fill bound to _level), so it pairs with geom-density-2d-filled or geom-polygon.

Either breaks, binwidth, or bins controls band placement; precedence runs breaks > binwidth > bins (default bins: 10).

from R’s bw.nrd / 4 (the seed MASS kde2d uses); pass a number for both axes or an (x, y) tuple.

Usage

stat-density-2d-filled(
  bw: auto,
  adjust: 1,
  n: 25,
  bins: 10,
  binwidth: none,
  breaks: auto,
)

Parameters

Parameter Default Description
bw auto Kernel standard deviation per axis. auto derives it per axis
adjust 1 Bandwidth multiplier: adjust: 0.5 halves the smoothing.
n 25 Grid resolution per axis: a number or an (x, y) tuple.
bins 10 Target band count when breaks and binwidth are unset.
binwidth none Fixed step between band edges. Overrides bins.
breaks auto Explicit array of band edges. Overrides bins and binwidth.

Returns

Statistic object with name: "density-2d-filled".

Outputs

  • x.
  • y.
  • group.
  • _level.

Examples

Shaded density bands of two point clouds.

#let d = range(0, 60).map(i => {
  let lobe = calc.rem(i, 2)
  (
    x: 2 + lobe * 4 + calc.sin(i * 1.7) * 0.8,
    y: 2 + lobe * 3 + calc.cos(i * 2.3) * 0.8,
  )
})
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-density-2d-filled(bins: 8),),
  scales: scales(
    x: scale-continuous(expand: (0, 0)),
    y: scale-continuous(expand: (0, 0)),
  ),
  width: 10cm,
  height: 6cm,
)

Filled density bands shading two point clouds centred near (2, 2) and (6, 5), darker where the estimated density is higher.

Filled density bands shading two point clouds centred near (2, 2) and (6, 5), darker where the estimated density is higher.

See also

geom-density-2d-filled, stat-contour-filled, stat-density-2d.

Back to top