scale-x-discrete

Discrete x scale: axis title, level ordering, and tick labels.

Usage

scale-x-discrete(
  name: none,
  limits: none,
  labels: auto,
  expand: auto,
)

Parameters

Parameter Default Description
name none Axis title. Overrides any name set via labs when both are present.
limits none Array of level names controlling order and inclusion, or none for first-seen order.
labels auto Array of tick labels aligned with limits, or auto.
expand auto Padding around the domain. Accepts a ratio (5%) for proportional breathing room, a length (5pt) for canvas-space padding, a relative (5pt + 5%) for both, or a (lo, hi) 2-tuple for asymmetric padding. auto keeps a 60%-of-slot default on each side; false collapses to zero.

Returns

Scale object consumed by plot.

Examples

Force the level order with limits so the bars sit in alphabetical order regardless of input.

#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: (scale-x-discrete(limits: ("a", "b", "c")),),
  width: 10cm,
  height: 6cm,
)

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

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

Provide custom labels to display human-friendly tick text without renaming the underlying data.

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

Bar chart with three bars on a discrete x axis whose tick labels read Alpha, Beta, Gamma while the underlying levels remain a, b, c.

Bar chart with three bars on a discrete x axis whose tick labels read Alpha, Beta, Gamma while the underlying levels remain a, b, c.

See also

scale-y-discrete, scale-x-continuous.

Back to top