scale-fill-discrete

Discrete fill scale mapping categorical levels to fill colours.

Usage

scale-fill-discrete(
  name,
  palette,
  limits,
  oob,
  labels,
)

Parameters

Parameter Default Description
name Legend title. Overrides any name set via labels when both are present.
palette Colour source: an array of colours, or auto.
limits Array of level names controlling order and inclusion, or none.
oob Out-of-range policy: "drop" (default) removes rows whose value falls outside limits; "squish" clamps continuous values to the nearest endpoint.
labels Array of legend labels aligned with limits, or auto.

Returns

Scale object consumed by plot.

Examples

Default palette filling three bars from the library’s reserved discrete colours.

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

Bar chart of three bars a, b, c each filled with one of the library's reserved discrete categorical colours.

Bar chart of three bars a, b, c each filled with one of the library's reserved discrete categorical colours.

The palette laid out as a swatch strip via geom-rect, one rectangle per level.

#let levels = ("a", "b", "c", "d", "e", "f", "g", "h")
#let d = levels.enumerate().map(((i, k)) => (
  xmin: i, xmax: i + 1, ymin: 0, ymax: 1, k: k,
))
#plot(
  data: d,
  mapping: aes(xmin: "xmin", xmax: "xmax", ymin: "ymin", ymax: "ymax", fill: "k"),
  layers: (geom-rect(),),
  scales: (scale-fill-discrete(),),
  guides: guides(fill: none),
  theme: theme-void(),
  width: 8cm,
  height: 1cm,
)

Legend swatch strip of eight rectangles displaying each reserved discrete fill colour in level order from a to h.

Legend swatch strip of eight rectangles displaying each reserved discrete fill colour in level order from a to h.

See also

scale-fill-manual, scale-fill-viridis-d, scale-colour-discrete.

Back to top