Interactions

library(gglite)
p = g2(mtcars, hp ~ mpg)

Interactions enable user-driven behaviors such as tooltips, brushing, and highlighting. Use interact() to add them.

1 Tooltip

p |> interact('tooltip')

2 Element Highlight

Highlight individual elements on hover.

pc = g2(mtcars, hp ~ mpg, color = ~ cyl)
pc |> interact('elementHighlight')

3 Element Highlight by X

Highlight all elements sharing the same x value.

df = data.frame(
  x = rep(c('A', 'B', 'C'), each = 2), y = c(3, 2, 5, 4, 1, 6),
  color = rep(c('a', 'b'), 3)
)
pd = g2(df, y ~ x, color = ~ color) |>
  mark_interval() |>
  transform('dodgeX')
pd |> interact('elementHighlightByX')

4 Element Highlight by Color

pd |> interact('elementHighlightByColor')

5 Element Select

Click to select individual elements.

pc |> interact('elementSelect')

6 Brush Highlight

Drag to brush and highlight a rectangular region.

p |> interact('brushHighlight')

7 Brush X Highlight

Brush only along the x axis.

p |> interact('brushXHighlight')

8 Brush Y Highlight

Brush only along the y axis.

p |> interact('brushYHighlight')

9 Brush Filter

Drag to filter the visible data.

p |> interact('brushFilter')

10 Legend Filter

Click legend entries to filter data.

p_iris = g2(iris, Sepal.Length ~ Sepal.Width, color = ~ Species)
p_iris |> interact('legendFilter')

11 Legend Highlight

Hover over legend entries to highlight.

p_iris |> interact('legendHighlight')

12 Multiple Interactions

You can combine several interactions.

p_iris |>
  interact('tooltip') |>
  interact('legendFilter') |>
  interact('brushHighlight')