annotate

Build a one-row annotation layer dispatching to a named geom.

Splits keyword arguments into two groups: those whose names match an aesthetic channel (see _aes-keys) become a single-row inline dataset plus an aesthetic mapping pointing each channel at the matching column, and the remainder are forwarded verbatim to the geom constructor as layer parameters. The resulting layer always has inherit-aes: false so it does not pick up the plot-level mapping.

Usage

annotate(
  geom,
  ..fields,
)

Parameters

Parameter Default Description
geom Geom name to dispatch to. One of "text", "point", "label", "segment", "rect", "vline", "hline", "abline".
..fields Named arguments split between aesthetics and layer parameters. Aesthetic names are x, y, xend, yend, xmin, xmax, ymin, ymax, colour, fill, size, alpha, shape, linetype, label, group, slope, intercept. For geom = "text" and geom = "label", size is treated as a layer parameter (the text size, a Typst length) rather than an aesthetic. Anything else (e.g., stroke, fontsize, xintercept, yintercept) is forwarded to the geom constructor as a layer parameter.

Returns

Layer dictionary consumed by plot.

Examples

Inline text plus a vertical reference line at the same x.

#let d = range(0, 10).map(i => (x: i, y: i * 0.5))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    geom-point(size: 2pt),
    annotate("text", x: 5, y: 4, label: "peak"),
    annotate("vline", xintercept: 5, colour: rgb("#cc0000")),
  ),
  width: 10cm,
  height: 6cm,
)

Scatter chart with a red vertical reference line at x = 5 and a 'peak' text annotation positioned beside it.

Scatter chart with a red vertical reference line at x = 5 and a 'peak' text annotation positioned beside it.

Highlight a region with a translucent rectangle and a boxed label callout.

#let d = range(0, 10).map(i => (x: i, y: i * 0.5))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    annotate(
      "rect",
      xmin: 3, xmax: 6, ymin: 1, ymax: 3.5,
      fill: rgb("#fff7e6"), alpha: 0.5,
    ),
    geom-point(size: 2pt),
    annotate("label", x: 4.5, y: 3.2, label: "window"),
  ),
  width: 10cm,
  height: 6cm,
)

Scatter chart with a pale yellow translucent rectangle highlighting the region x in 3-6, y in 1-3.5 and a boxed 'window' label inside.

Scatter chart with a pale yellow translucent rectangle highlighting the region x in 3-6, y in 1-3.5 and a boxed 'window' label inside.

See also

aes, geom-text, geom-point, geom-label, geom-segment, geom-rect, geom-vline, geom-hline, geom-abline.

Back to top