Segment

Layers are declared with the DRAW clause. Read the documentation for this clause for a thorough description of how to use it.

The segment layer is used to create line segments between two endpoints. It differs from lines and paths in that it connects just two points rather than many. Data is expected to be in a different shape, with 4 coordinates for the start (e.g. x/y) and end (e.g. xend/yend) points on a single row.

Aesthetics

The following aesthetics are recognised by the segment layer.

Required

  • Primary axis (e.g. x): Start position along the primary axis.
  • Primary axis end (e.g. xend): End position along the primary axis.
  • Secondary axis (e.g. y): Start position along the secondary axis.
  • Secondary axis end (e.g. yend): end position along the secondary axis.

* Only one of end value is strictly required. If one is missing, it takes on the value of the start point.

Optional

  • colour/stroke: The colour of the line.
  • opacity: The opacity of the line.
  • linewidth: The width of the line.
  • linetype: The type of the line, i.e. the dashing pattern.

Settings

  • position: Position adjustment. One of 'identity' (default), 'stack', 'dodge', or 'jitter'

Data transformation

The segment layer does not transform its data but passes it through unchanged.

Orientation

The segment layer has no orientations. The axes are treated symmetrically.

Examples

Segments are useful when you have known start and end points of the data. For example in a graph

WITH edges(x, y, xend, yend, type) AS (VALUES
    (0, 0, 1, 1, 'A'),
    (1, 1, 2, 1, 'A'),
    (2, 1, 3, 0, 'A'),
    (0, 3, 1, 2, 'B'),
    (1, 2, 2, 2, 'B'),
    (2, 2, 3, 3, 'B'),
    (1, 1, 1, 2, 'C'),
    (2, 2, 2, 1, 'C')
)
VISUALISE x, y, xend, yend FROM edges
DRAW segment 
  MAPPING type AS stroke

You can use segments as part of a lollipop chart by anchoring one of the ends to 0. Note that xend is missing and has taken up the value of x.

SELECT ROUND(bill_dep) AS bill_dep, COUNT(*) AS n 
FROM ggsql:penguins 
GROUP BY ROUND(bill_dep)

VISUALISE bill_dep AS x, n AS y
DRAW segment 
  MAPPING 0 AS yend
DRAW point

By overlaying a thick line on a thin line, you can create a candlestick chart.

SELECT
  FIRST(Date) AS date,
  FIRST(temp) AS open,
  LAST(temp) AS close,
  MAX(temp) AS high,
  MIN(temp) AS low,
  CASE
    WHEN FIRST(temp) > LAST(temp) THEN 'colder'
    ELSE 'warmer'
  END AS trend
FROM ggsql:airquality
GROUP BY Week

VISUALISE date AS x, trend AS colour
DRAW segment 
  MAPPING open AS y, close AS yend
  SETTING linewidth => 5
DRAW segment 
  MAPPING low AS y, high AS yend