scale-discrete

Discrete scale for any categorical aesthetic.

Keyed to x/y it orders the axis levels; keyed to colour/fill, shape, or linetype it assigns the per-level palette, defaulting to that aesthetic’s built-in set.

Usage

scale-discrete(
  ..args,
)

Parameters

Parameter Default Description
..args Named arguments forwarded to the bound scale.

Keys accepted by ..args:

  • name: Axis or legend title, as a string or content; none (default) falls back to the mapped column name.
  • limits: Array of level names selecting which levels appear and in which order; none (default) keeps the trained levels. A level name must be a string, because a discrete scale reads a bare number as a position rather than as a level, so quote a numeric level as "1".
  • oob: Handling of rows whose level falls outside limits: "drop" (default) removes them; "squish" censors too, since clamping has no meaning on levels.
  • labels: Array of labels in level order, a function of the level returning a string or content, or auto (default) to print the level itself.
  • expand: Padding around the domain as a ratio (5%), a length (5pt), a relative, or a (lo, hi) pair whose sides may each be auto; false or none removes it. auto (default) pads 0.6 of a level slot per side. x/y only.
  • palette: Array of output values, one per level and recycled when shorter than the level count: colours for colour/fill, marker keywords for shape, dash keywords for linetype. auto (default) uses that aesthetic’s built-in set.

Returns

Deferred scale spec consumed by scales.

Examples

Force the level order on a discrete x axis.

#let d = ((grp: "b", y: 3), (grp: "a", y: 5), (grp: "c", y: 2))
#plot(
  data: d,
  mapping: aes(x: "grp", y: "y"),
  layers: (geom-col(),),
  scales: scales(x: scale-discrete(limits: ("a", "b", "c"))),
  width: 10cm,
  height: 6cm,
)

Bar chart with three categorical bars ordered a, b, c on a discrete x axis with heights 5, 3, 2.

Bar chart with three categorical bars ordered a, b, c on a discrete x axis with heights 5, 3, 2.

See also

scales, scale-continuous, scale-manual.

Back to top