p |> select_point("hi", on = "hover") # hover a mark
p |> select_interval("brush", encodings = "x") # drag an x-rangeMost of vellumplot describes a static picture. But interaction can be part of the plot too — declared in the spec, travelling with it, and enacted by any capable host (today, vellumwidget). This is different from configuring a widget after the fact: the interaction is a first-class, serialisable piece of the plot.
Everything on this page is inert on a static render — a plot with interactions compiles and draws exactly like one without (the figures below are the static renders). The interaction comes alive only when a host such as vellumwidget::as_widget() enacts it.
Selections
A selection is a named set of data elements defined by a user gesture. On its own it does nothing; you refer to it elsewhere. Declare one with select_point() (a click or hover, or a legend value) or select_interval() (a brush over a data range):
on sets a point selection’s gesture; empty = TRUE (the default) means an empty selection contains everything — so an untouched plot shows its full self and interacting narrows it.
Conditional encoding: condition()
Use condition() as the value of an aesthetic to make it depend on selection membership: members get the first value, non-members the second.
df <- data.frame(x = mtcars$wt, y = mtcars$mpg, cyl = factor(mtcars$cyl))
vplot(df) |>
mark_point(x = x, y = y, color = condition("hi", cyl, "grey80")) |>
select_point("hi", on = "hover")condition() is transparent to the grammar: the if_true branch trains the colour scale and draws the legend exactly as color = cyl would, so the static render is the full, meaningful plot. With empty = TRUE, nothing is selected initially, so every point shows its if_true colour; once a host activates the selection, non-members switch to if_false (here "grey80") — the spotlight.
Omit if_false to fall back to the theme’s dim appearance:
mark_point(x = x, y = y, color = condition("hi", cyl)) # non-members just dimBrushing a range: select_interval()
select_interval() fills a selection by dragging a brush. The host converts the brushed pixels to a data range through the panel’s scale, and every element whose data value falls in that range is a member. encodings picks the channels the brush constrains: "x" (the default, a full-height band), "y", or c("x", "y") for a rectangle.
econ <- data.frame(
date = as.Date("2000-01-01") + seq(0, by = 30, length.out = 60),
value = cumsum(sin(seq_len(60) / 4)) + 10
)
econ$year <- format(econ$date, "%Y")
vplot(econ) |>
mark_point(x = date, y = value, color = condition("when", year, "grey80")) |>
select_interval("when", encodings = "x")Because membership is by value, the brush follows the scale rather than the drawn shapes:
- on a continuous axis (log, sqrt and reversed axes too) an element is a member when its value lies in the brushed range;
- on a date or datetime axis the same test runs on the date’s numeric value;
- on a discrete axis a category is selected when the brush covers the centre of its band.
In a faceted plot the range applies to every panel, so brushing one facet picks out the matching rows in all of them. Clicking empty space (or pressing Escape) clears the brush and empties the selection. Under Shiny, the members arrive in input$<id>_selected and the brushed data range in input$<id>_brush.
Filtering: filter_by()
filter_by() shows only the selection’s members, hiding the rest:
vplot(df) |>
mark_point(x = x, y = y) |>
select_interval("brush", encodings = c("x", "y")) |>
filter_by("brush")Cross-filtering across views
The real power is pointing a second view at a selection defined on a first — brush one panel, a linked panel narrows to those rows while the source stays full. Define the selection once, add_selection() it on the source, and filter_by() it on the target:
sel <- select_interval("brush", encodings = c("x", "y"))
hconcat(
vplot(df) |> mark_point(x = x, y = y) |> add_selection(sel),
vplot(df) |> mark_point(x = y, y = x) |> filter_by(sel)
)The two views share row identity, so a gesture in one maps to the matching rows in the other. This cross-view coordination is exactly what a widget flag cannot express — it lives in the plot.
Overview + detail: bind_scale()
bind_scale() binds a view’s scale domain to an interval selection defined on another view. Brush the small overview and the detail zooms to the brushed range; clear the brush (click empty space, or Escape) and the detail zooms back out. This is Vega-Lite’s overview + detail, where the detail’s scale.domain names the brush:
econ <- ggplot2::economics
brush <- select_interval("brush", encodings = "x")
overview <- vplot(econ) |>
mark_area(x = date, y = unemploy, fill = "steelblue", alpha = 0.6) |>
add_selection(brush) |>
labs(x = NULL, y = NULL)
detail <- vplot(econ) |>
mark_line(x = date, y = unemploy, color = "steelblue") |>
bind_scale(brush, aes = "x") |>
labs(x = NULL, y = "Unemployed (thousands)")
vconcat(detail, overview, heights = c(3, 1))The detail keeps its own scales; the host only changes what part of them is visible and re-ticks the axes for that range (a display-tier zoom, nothing is recomputed). Bind several views to one brush, or bind both "x" and "y" to a rectangular brush. The view the brush is drawn in never zooms itself. Under Shiny, every panel’s visible range arrives in input$<id>_zoom$panels, keyed by panel name; a composition names its panels per cell ("cell-1-panel-1-1" is the detail here, "cell-2-panel-1-1" the overview).
Linked zoom: bind = "scales"
select_interval(bind = "scales") is Vega-Lite’s other half: no brush is drawn. The selection is the view’s own pan/zoom domain, so zooming or panning the view (wheel, pinch, the navigator, a drag in pan mode, which becomes the default drag) writes the interval, and resetting the zoom empties it. A bind_scale() on another view then follows it (linked zoom), and condition() / filter_by() react to whatever is in view:
zoom <- select_interval("zoom", encodings = "x", bind = "scales")
hconcat(
vplot(econ) |> mark_line(x = date, y = unemploy) |> add_selection(zoom),
vplot(econ) |> mark_line(x = date, y = psavert) |> bind_scale(zoom)
)Which marks are addressable
Give a mark an identity with data_id= (and, optionally, a tooltip=) and it becomes hoverable, selectable and cross-filterable. This is no longer limited to the batched marks (points, bars, tiles, segments): a line or step series is one addressable object, a filled mark_area() / mark_ribbon() band is one polygon, mark_text() labels are addressable per datum, mark_label() keys by its rounded background box, and an sf polygon / choropleth keys per feature.
# hover the whole trend line as one object
vplot(pressure) |>
mark_line(x = temperature, y = pressure, data_id = "vapor-pressure") |>
select_point("series", on = "hover")A mark you leave without a key is simply not addressable — and, for lines, polygons and text, it contributes no row to the interaction model at all. That is deliberate: a plot’s gridlines and axis labels are unkeyed text and lines, and they must not drown the marks that carry meaning. So absence of a key is a choice you are making, not a limitation.
Default keys
A layer without data_id still gets keys when it has a tooltip, a condition(), or the plot declares a selection. These default keys name the panel, the layer and the row: "p1-1:l2:7" is row 7 of layer 2 in the first (or only) panel, and a line or area series takes the key of its first row. In a composition the cell id goes in front ("subplot-2:p1-1:l1:7"). So no two layers ever share a default key, and selecting a point never selects a line by accident. When two layers should react together, give them the same data_id:
# clicking a point selects its whole series, line included
vplot(df) |>
mark_line(x = t, y = v, color = g, data_id = g) |>
mark_point(x = t, y = v, data_id = g) |>
select_point("series")What a host does with it
vellumplot compiles once and carries the interaction declarations plus the per-element metadata a host needs (interaction_model() returns the declaration block). The host reads them and wires the gestures it already performs on the frozen scene — highlight, hide, pan/zoom — with no recompilation. Turn any of the plots above into a live widget with:
library(vellumwidget)
vplot(df) |>
mark_point(x = x, y = y, color = condition("hi", cyl, "grey80")) |>
select_point("hi", on = "hover") |>
as_widget()The interaction is display-tier by design: it reacts on the frozen scene (highlight, filter-by-hiding, pan/zoom) and never recomputes the grammar. See select_point(), select_interval(), condition(), filter_by(), add_selection(), and bind_scale() in the reference.
