geom-abline

Straight reference line described by slope and intercept.

The line runs across the full trained x domain. Requires continuous x and y scales; discrete scales are skipped silently.

Usage

geom-abline(
  slope: 1,
  intercept: 0,
  colour: auto,
  stroke: 0.6pt,
  alpha: auto,
  linetype: "solid",
  inherit-aes: false,
)

Parameters

Parameter Default Description
slope 1 Line slope.
intercept 0 Line y intercept.
colour auto Line colour. auto inherits the theme ink.
stroke 0.6pt Line thickness (a Typst length).
alpha auto Line opacity in [0, 1].
linetype "solid" Dash keyword. Defaults to "solid".
inherit-aes false Whether to merge the plot-level mapping into this layer’s mapping. Defaults to false.

Returns

Layer dictionary consumed by plot.

Examples

The y = x identity reference line over a noisy point cloud.

#let d = range(0, 10).map(i => (x: i, y: i + calc.rem(i, 2)))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    geom-point(size: 2pt),
    geom-abline(slope: 1, intercept: 0, colour: rgb("#cc0000")),
  ),
  width: 10cm,
  height: 6cm,
)

Scatter of x (0 to 9) against y (i + i mod 2) overlaid with a red y = x identity reference line.

Scatter of x (0 to 9) against y (i + i mod 2) overlaid with a red y = x identity reference line.

Adjust slope and intercept to anchor a custom regression reference.

#let d = range(0, 10).map(i => (x: i, y: 0.7 * i + 1 + calc.sin(i)))
#plot(
  data: d,
  mapping: aes(x: "x", y: "y"),
  layers: (
    geom-point(size: 2pt),
    geom-abline(slope: 0.7, intercept: 1, colour: rgb("#1b9e77")),
  ),
  width: 10cm,
  height: 6cm,
)

Scatter of x against noisy y values with a green reference line at slope 0.7 and intercept 1.

Scatter of x against noisy y values with a green reference line at slope 0.7 and intercept 1.

See also

geom-hline, geom-vline, geom-smooth.

Back to top