scale-colour-gradient2

Continuous diverging colour gradient through a midpoint.

Interpolates low to mid for values at or below midpoint, and mid to high for values at or above it.

Usage

scale-colour-gradient2(
  low,
  mid,
  high,
  midpoint,
  name,
  limits,
  oob,
  breaks,
  labels,
)

Parameters

Parameter Default Description
low Colour for values far below midpoint.
mid Colour at midpoint.
high Colour for values far above midpoint.
midpoint Value at which the palette transitions through mid.
name Legend title. Overrides any name set via labels when both are present.
limits Pair (lo, hi) clipping the trained domain, or none.
oob Out-of-range policy: "drop" (default) removes rows whose value falls outside limits; "squish" clamps continuous values to the nearest endpoint.
breaks Array of break values for the legend, or auto.
labels Array of legend labels aligned with breaks, or auto.

Returns

Scale object consumed by plot.

Examples

Default diverging ramp pivoting at zero, useful for signed numeric values.

#let d = range(-5, 6).map(i => (x: i, y: i, z: i))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y", colour: "z"),
  layers: (geom-point(size: 3pt),),
  scales: (scale-colour-gradient2(midpoint: 0),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of eleven points coloured by z along a diverging ramp that pivots through white at zero, blue for negative and red for positive.

Scatter chart of eleven points coloured by z along a diverging ramp that pivots through white at zero, blue for negative and red for positive.

Shift midpoint to centre the ramp around a non-zero baseline.

#let d = range(0, 11).map(i => (x: i, y: i, z: i))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y", colour: "z"),
  layers: (geom-point(size: 3pt),),
  scales: (scale-colour-gradient2(
    low: rgb("#1a9850"),
    mid: rgb("#ffffbf"),
    high: rgb("#d73027"),
    midpoint: 5,
  ),),
  width: 10cm,
  height: 6cm,
)

Scatter chart of eleven points coloured by z along a green-yellow-red diverging ramp pivoting through pale yellow at midpoint 5.

Scatter chart of eleven points coloured by z along a green-yellow-red diverging ramp pivoting through pale yellow at midpoint 5.

See also

scale-colour-gradient, scale-colour-gradientn, scale-fill-gradient2.

Back to top