scale-x-binned

Binned continuous x scale: quantises a numeric axis into n-breaks bins.

Keeps the underlying mapping continuous so geoms still receive their raw numeric position, but places ticks at the midpoint of each equal-width bin to communicate the discretised reading of the axis.

Usage

scale-x-binned(
  name: none,
  limits: none,
  oob: "drop",
  n-breaks: 10,
  breaks: auto,
  labels: auto,
)

Parameters

Parameter Default Description
name none Axis title. Overrides any name set via labels when both are present.
limits none Pair (lo, hi) clipping the trained domain, or none. Either element may be auto to keep the trained bound on that side.
oob "drop" Out-of-range policy: "drop" (default) removes rows whose value falls outside limits; "squish" clamps them to the nearest endpoint.
n-breaks 10 Number of bins to partition the domain into. Ignored when breaks is set.
breaks auto Array of bin edges, or auto to derive equal-width bins from n-breaks. Edges define the bin boundaries; ticks sit at each interval midpoint and the domain widens to cover them.
labels auto Array of tick labels aligned with the bin midpoints, or auto.

Returns

Scale object consumed by plot.

Examples

Five equal-width bins along the x axis.

#let d = range(0, 30).map(i => (x: i / 3.0, y: calc.sin(i / 4.0)))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-point(size: 2pt),),
  scales: (scale-x-binned(n-breaks: 5),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of a sinusoidal series along an x axis cut into five equal-width bins whose midpoints place the tick labels.

Scatter chart of a sinusoidal series along an x axis cut into five equal-width bins whose midpoints place the tick labels.

Bump n-breaks for a finer grid; pair with limits to focus on a sub-range.

#let d = range(0, 30).map(i => (x: i / 3.0, y: calc.sin(i / 4.0)))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (geom-point(size: 2pt),),
  scales: (scale-x-binned(n-breaks: 10, limits: (2, 8)),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of a sinusoidal series along an x axis cut into ten finer bins and clipped to limits 2 through 8 to zoom into a sub-range.

Scatter chart of a sinusoidal series along an x axis cut into ten finer bins and clipped to limits 2 through 8 to zoom into a sub-range.

See also

scale-y-binned, scale-x-continuous.

Back to top