stat-density-ridges

Ridgeline statistic: Gaussian kernel density estimate of x per y bucket.

For every distinct y value, emits n evenly spaced rows along x where height is the estimated density scaled to a shared maximum of 1 across all buckets, plus the after-stat columns _density (estimated density), _scaled (density scaled per bucket), and _n (the bucket’s observation count).

(R’s bw.nrd0); pass a positive number to fix it.

adjust: 0.5 halves the smoothing.

per bucket.

false (default) extends the grid by three bandwidths on each side so every ridge decays to its baseline.

Usage

stat-density-ridges(
  bw: auto,
  adjust: 1,
  n: 512,
  trim: false,
)

Parameters

Parameter Default Description
bw auto Kernel bandwidth. auto applies Silverman’s rule of thumb
adjust 1 Bandwidth multiplier: the kernels use adjust * bw, so
n 512 Number of evenly spaced grid points the density is evaluated at
trim false Whether to restrict each curve to its bucket’s x range.

Returns

Statistic object with name: "density-ridges", consumed by geom-density-ridges.

Outputs

  • x.
  • y.
  • height.
  • _density.
  • _scaled.
  • _n.

Examples

Sharper ridges via adjust on the backing statistic.

#let xs = (1, 2, 2, 3, 5, 6, 6, 7)
#let d = ()
#for (i, grp) in ("a", "b", "c").enumerate() {
  for x in xs {
    d.push((grp: grp, x: x + i))
  }
}
#plot(
  data: d,
  mapping: aes(x: "x", y: "grp"),
  layers: (geom-density-ridges(scale: 1.4, stat: stat-density-ridges(adjust: 0.5)),),
  scales: scales(y: scale-discrete(expand: (auto, 45%))),
  width: 10cm,
  height: 6cm,
)

Three overlapping ridgeline densities for groups a, b, c on the y-axis over values on the x-axis, smoothed with a halved bandwidth so each ridge shows two sharp peaks.

Three overlapping ridgeline densities for groups a, b, c on the y-axis over values on the x-axis, smoothed with a halved bandwidth so each ridge shows two sharp peaks.

See also

geom-density-ridges, stat-density, stat-ydensity.

Back to top