Rule

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

The rule layer is used to draw horizontal or vertical reference lines at specified values. This is useful for adding thresholds, means, medians, avent markers, cutoff dates or other guides to the plot. The lines span the full width or height of the panels.

Aesthetics

The following aesthetics are recognised by the hline layer.

Required

  • x*: The x-coordinate for the vertical line.
  • y*: The y-coordinate for the horizontal line

* Exactly one of x or y is required, not both.

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

The rule layer has no additional settings.

Data transformation

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

Examples

Add a horizontal threshold line to a time series plot:

SELECT Date AS date, temp AS temperature
FROM ggsql:airquality
WHERE Month = 5

VISUALISE 
  DRAW line MAPPING date AS x, temperature AS y
  DRAW rule MAPPING 70 AS y

Add a vertical line to mark a specific value:

VISUALISE FROM ggsql:penguins
  DRAW point MAPPING bill_len AS x, bill_dep AS y
  DRAW rule MAPPING 45 AS x

Add multiple threshold lines with different colors:

WITH thresholds AS (
    SELECT * FROM (VALUES
        (70, 'Target'),
        (80, 'Warning'),
        (90, 'Critical')
    ) AS t(value, label)
)
SELECT Date AS date, temp AS temperature
FROM ggsql:airquality
WHERE Month = 5

VISUALISE 
  DRAW line MAPPING date AS x, temperature AS y
  DRAW rule MAPPING value AS y, label AS colour FROM thresholds