scale-colour-discrete

Discrete colour scale mapping categorical levels to stroke colours.

Usage

scale-colour-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 mapping three categories to the library’s eight reserved discrete colours.

#let d = (
  (x: 1, y: 2, sp: "a"),
  (x: 2, y: 4, sp: "b"),
  (x: 3, y: 3, sp: "c"),
)
#plot(
  data: d,
  mapping: aes(x: "x", y: "y", colour: "sp"),
  layers: (geom-point(size: 3pt),),
  scales: (scale-colour-discrete(),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of three points where sp maps to three distinct categorical colours from the library's reserved discrete palette.

Scatter chart of three points where sp maps to three distinct categorical colours from the library's reserved discrete palette.

Override palette with an explicit colour array to hand-pick the mapping order.

#let d = (
  (x: 1, y: 2, sp: "a"),
  (x: 2, y: 4, sp: "b"),
  (x: 3, y: 3, sp: "c"),
)
#plot(
  data: d,
  mapping: aes(x: "x", y: "y", colour: "sp"),
  layers: (geom-point(size: 3pt),),
  scales: (scale-colour-discrete(palette: (
    rgb("#1b9e77"), rgb("#d95f02"), rgb("#7570b3"),
  )),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of three points where sp maps to a hand-picked teal, orange, and blue trio supplied directly through palette.

Scatter chart of three points where sp maps to a hand-picked teal, orange, and blue trio supplied directly through palette.

Bind a colour-blind-friendly palette to penguin species and reorder the legend via limits.

#plot(
  data: penguins,
  mapping: aes(x: "flipper-len", y: "body-mass", colour: "species"),
  layers: (geom-point(size: 2pt, alpha: 0.85),),
  scales: (scale-colour-discrete(
    palette: (rgb("#0072B2"), rgb("#D55E00"), rgb("#009E73")),
    limits: ("Adelie", "Chinstrap", "Gentoo"),
  ),),
  labels: labels(x: "Flipper Length (mm)", y: "Body Mass (g)", colour: "Species"),
  width: 11cm,
  height: 6cm,
)

Scatter chart of penguin flipper length against body mass coloured by species using a blue-orange-green colour-blind-friendly palette with limits fixing legend order.

Scatter chart of penguin flipper length against body mass coloured by species using a blue-orange-green colour-blind-friendly palette with limits fixing legend order.

See also

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

Back to top