geom.rule(), added to the spec with its own value and style.
A horizontal goal line
Bind the rule to a constanty with aes: { y: { value } }. The { value } form is a constant mapping: it pins the aesthetic to a literal instead of a column.
The label
A rule’sparams carry its label and nothing else:
Under coord.flip() a rule runs vertically, and 'end'
puts the label at the top of the line rather than at its foot. ToggleGoalLineCommand and
SetStatLineCommand pick that end for you when they build a line under a flipped coord.
Painting the line
Stroke and label type come from the stylesheet, like every other geom.style.geom.rule() takes stroke, strokeWidth, dashArray, strokeAlpha, lineCap, lineJoin, shadow and
blendMode; style.geom.rule.label() takes fontFamily, fontSize, fontWeight, lineHeight, textColor and the
other text properties. Both take a { layer } option, so one rule can be painted without touching the others. Leave
textColor undeclared and the label’s text color is picked to read against the pill.
ruleLine token, so a bare
geom.rule() draws a dashed line. Redefine ruleLine through styles({ tokens: { ruleLine } })
to recolour every rule, goal line and average line at once.
Vertical lines
Mark a constant along the x-axis withaes: { x: { value } } instead:
Date, an ISO string, or the value as the x column writes it (2010 or '2010' on a year column). The rule reads it in the column’s format, sits on that instant, and its label reads the date in the axis’s value format: the full date, where the ticks may show only the year. A string that is neither in the column’s format nor an ISO instant is an INVALID_RULE_MAPPING error:
A vertical rule needs a numeric or date x-axis: it marks a constant x-value,
which has no place between bands. Band x-axes support horizontal rules only. A
rule reads one value from exactly one axis, so supplying both
x and y is
an INVALID_RULE_MAPPING error, as is supplying neither.Multiple rules
Rules compose like any other layer and paint in spec order: a rule added aftergeom.bar() draws in front of the bars. Add as many as you need:
{ layer } entry addresses the layer’s id.
Flipped and secondary axes
A rule tracks the data space, not the screen. Undercoord.flip() a { y: { value } } rule still marks a constant y; the renderer paints it as a vertical line because the y-axis now runs horizontally. To pin a rule to a secondary axis, add yScaleType: 'secondary':
An average line
A rule’s value can also be computed. Give itstat.mean() instead
of a constant and the line sits at the layer’s average:
Editing a rule at runtime
Four commands cover reference lines in an editing UI:Related
- Styling — the stylesheet entries that paint a rule and its label
- Statistics —
stat.mean()and the fitted trends a rule can carry - Mappings & aesthetics — the
{ value }constant form - Scales — how the rule value extends the auto-domain
- Coordinate systems — rules under
coord.flip()

