Skip to contents

Declare the scale for a mapped linewidth aesthetic on a stroked mark – mark_line(), mark_step(), or mark_segment(). Data values are rescaled linearly onto a line-width range (in lwd units, as everywhere else in the package) and a line-width legend is drawn automatically.

Usage

scale_linewidth(plot, range = NULL, limits = NULL, breaks = NULL, name = NULL)

scale_linewidth_continuous(
  plot,
  range = NULL,
  limits = NULL,
  breaks = NULL,
  name = NULL
)

Arguments

plot

A PlotSpec.

range

Output line-width range c(min, max), or NULL for the default c(0.5, 4).

limits

Data limits c(min, max), or NULL to train from the data.

breaks

Explicit legend breaks, or NULL.

name

Legend title, or NULL to derive from the encoding.

Value

The modified PlotSpec.

Details

On mark_line() and mark_step(), a mapped linewidth draws each series as one continuous stroke whose width tapers smoothly between the values at its vertices (via vellum's lines_grob(lwd_profile = )). The stroke is built as a filled outline at render time, so it looks the same in PNG, SVG and PDF and stays one element per series in vellum::scene_model() and the SVG. A constant linewidth = is unaffected and still draws an ordinary stroke.

A tapered stroke has one colour and no dash pattern, so a series falls back to one segment per span – each at the mean of its two endpoint widths, so the width steps at every vertex – when it also maps a continuous colour that varies along the series (each segment then takes its own colour), uses a dashed or dotted linetype, is drawn with a sketch theme, or has a missing width. That fallback emits one path element per segment, worth knowing before combining those with tens of thousands of rows. mark_segment() is already one element per row and takes one width per segment.

For edges on a vgraph() plot, use scale_edge_width() instead: an edge's width is trained and legended independently of the line-width scale. That also means guides(linewidth = ) and lims(linewidth = ) address this scale; use guides(edge_width = ) / lims(edge_width = ) for a graph's edges.

A missing (NA) width takes its segment's width from whichever endpoint is known, so one missing value thins the line rather than erasing the two segments that meet at it. A segment missing a width at both ends is dropped. (A series with a missing width is drawn per segment, as above.)

Examples

vplot(pressure) |>
  mark_line(x = temperature, y = pressure, linewidth = pressure) |>
  scale_linewidth(range = c(0.5, 5))