geom-segment

Segment layer: one line from (x, y) to (xend, yend) per row.

Mapping must provide x, y, xend, yend. Colour and linetype may be mapped or set as fixed layer parameters.

Usage

geom-segment(
  mapping: none,
  data: none,
  stroke: auto,
  colour: auto,
  alpha: auto,
  linetype: "solid",
  stat: "identity",
  position: "identity",
  inherit-aes: true,
)

Parameters

Parameter Default Description
mapping none Layer-specific aesthetic mapping built with aes. Must map x, y, xend, yend.
data none Layer-specific dataset, or a function applied to the plot data returning the layer frame. Falls back to the plot data when none.
stroke auto Line thickness (a Typst length).
colour auto Fixed line colour. auto resolves via the colour scale.
alpha auto Line opacity in [0, 1].
linetype "solid" Dash keyword. Defaults to "solid".
stat "identity" Statistical transform name. Usually "identity".
position "identity" Position adjustment name. Usually "identity".
inherit-aes true Whether to merge the plot-level mapping into this layer’s mapping.

Returns

Layer dictionary consumed by plot.

Examples

Three crossing segments specified by their two endpoints.

#let d = (
  (x: 0, y: 0, xend: 4, yend: 3),
  (x: 0, y: 3, xend: 4, yend: 0),
  (x: 2, y: 0, xend: 2, yend: 3),
)
#plot(
  data: d,
  mapping: aes(x: "x", y: "y", xend: "xend", yend: "yend"),
  layers: (geom-segment(stroke: 1pt),),
  width: 10cm,
  height: 6cm,
)

Three straight line segments crossing through (x, y) and (xend, yend) endpoints forming an X plus vertical segment.

Three straight line segments crossing through (x, y) and (xend, yend) endpoints forming an X plus vertical segment.

Map colour and linetype to colour-coded categorical segments.

#let d = (
  (x: 0, y: 0, xend: 4, yend: 3, k: "a"),
  (x: 0, y: 3, xend: 4, yend: 0, k: "b"),
  (x: 2, y: 0, xend: 2, yend: 3, k: "a"),
)
#plot(
  data: d,
  mapping: aes(x: "x", y: "y", xend: "xend", yend: "yend", colour: "k", linetype: "k"),
  layers: (geom-segment(stroke: 1pt),),
  width: 10cm,
  height: 6cm,
)

Three line segments between (x, y) and (xend, yend) coloured and dashed by group (a, b) via colour and linetype aesthetics.

Three line segments between (x, y) and (xend, yend) coloured and dashed by group (a, b) via colour and linetype aesthetics.

Dumbbell connectors on a discrete y axis: x, xend, or yend resolve through the trained scale, so categorical slots work.

#let d = (
  (lo: 1, hi: 4, grp: "alpha"),
  (lo: 2, hi: 5, grp: "beta"),
  (lo: 3, hi: 6, grp: "gamma"),
)
#plot(
  data: d,
  mapping: aes(x: "lo", y: "grp", xend: "hi", yend: "grp"),
  layers: (geom-segment(stroke: 2pt),),
  scales: (scale-y-discrete(),),
  width: 10cm,
  height: 6cm,
)

Three horizontal connectors, one per category, each spanning a start value to an end value on a discrete y axis.

Three horizontal connectors, one per category, each spanning a start value to an end value on a discrete y axis.

See also

geom-line, geom-path.

Back to top