stat-bin

Bin statistic: partition x into uniform-width bins, count rows per bin.

Either bins or binwidth fixes the partition; if both are supplied, binwidth wins.

Usage

stat-bin(
  bins: 30,
  binwidth: none,
)

Parameters

Parameter Default Description
bins 30 Target number of bins when binwidth is none.
binwidth none Fixed bin width. Overrides bins when set.

Returns

Statistic object with name: "bin", consumed by geom layers.

Outputs

  • x.
  • y.
  • width.
  • _count.
  • _density.

Examples

Histogram driven by an eight-bin partition.

#let d = range(0, 40).map(i => (x: i * 0.25))
#plot(
  data: d,
  mapping: aes(x: "x"),
  layers: (geom-histogram(bins: 8),),
  width: 10cm,
  height: 6cm,
)

Histogram with x on the horizontal axis and count on the vertical axis, partitioned into eight equal-width bins covering a uniform range.

Histogram with x on the horizontal axis and count on the vertical axis, partitioned into eight equal-width bins covering a uniform range.

Constructor form: stat: stat-bin() is equivalent to stat: "bin" with defaults (bins: 30). Use the constructor to customise the partition on any geom, not just geom-histogram.

#let d = range(0, 40).map(i => (x: i * 0.25))
#plot(
  data: d,
  mapping: aes(x: "x"),
  layers: (geom-col(stat: stat-bin(bins: 8)),),
  width: 10cm,
  height: 6cm,
)

Bar chart with x on the horizontal axis and bin count on the vertical axis, using stat-bin with eight bins to drive geom-col.

Bar chart with x on the horizontal axis and bin count on the vertical axis, using stat-bin with eight bins to drive geom-col.

See also

geom-histogram, stat-count.

Back to top