stat-summary-bin

Summary statistic over uniform x bins.

Partitions x into uniform-width bins (same rule as stat-bin), then for every bin reduces the y values inside to a (x, y, ymin, ymax) row where x is the bin midpoint. The reduction is chosen by fun; supported names are "mean-se", "mean-cl-normal", "mean-sd", and "median-hilow".

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

Usage

stat-summary-bin(
  fun: "mean-se",
  bins: 30,
  binwidth: none,
  fun-args: (:),
)

Parameters

Parameter Default Description
fun "mean-se" Name of the summary helper to apply to each bin’s y values.
bins 30 Target number of bins when binwidth is none.
binwidth none Fixed bin width. Overrides bins when set.
fun-args (:) Keyword arguments forwarded to the helper.

Returns

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

Outputs

  • x.
  • y.
  • ymin.
  • ymax.

Examples

Mean and standard-error bands per bin, drawn as a polyline.

#let d = range(0, 80).map(i => (x: i / 10, y: calc.sin(i / 10) + i / 80))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    geom-line(stat: stat-summary-bin(fun: "mean-se", bins: 8)),
  ),
  width: 10cm,
  height: 6cm,
)

Line chart with x on the horizontal axis and y on the vertical axis, connecting the per-bin mean across eight equal-width bins with mean and standard-error summaries.

Line chart with x on the horizontal axis and y on the vertical axis, connecting the per-bin mean across eight equal-width bins with mean and standard-error summaries.

stat: "summary_bin" is equivalent to stat: stat-summary-bin() with defaults (fun: "mean-se", bins: 30). Use the constructor to customise the reduction or partition.

#let d = range(0, 80).map(i => (x: i / 10, y: calc.sin(i / 10) + i / 80))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    geom-pointrange(
      size: 3pt,
      stat: stat-summary-bin(fun: "median-hilow", bins: 8),
    ),
  ),
  width: 10cm,
  height: 6cm,
)

Pointrange chart with x on the horizontal axis and y on the vertical axis, showing the per-bin median and a hilow interval across eight equal-width x bins.

Pointrange chart with x on the horizontal axis and y on the vertical axis, showing the per-bin median and a hilow interval across eight equal-width x bins.

See also

stat-summary, stat-bin.

Back to top