Skip to main content

2  What can be interactive

library(ggplot2)
library(ggiraph)
library(patchwork)

The interactive aesthetics tooltip, data_id and onclick apply to every part of a ggplot, not only to its geometries. Each part has its own hover and selection options, and its own reactive value in Shiny:

Part Made interactive with Hover Selection Shiny input
geometries, facet strips geom_*_interactive(), facet_*_interactive() opts_hover() opts_selection() input$<id>_selected
legends scale_*_interactive(), guide_*_interactive() opts_hover_key() opts_selection_key() input$<id>_key_selected
titles, theme elements label_interactive(), element_*_interactive() opts_hover_theme() opts_selection_theme() input$<id>_theme_selected

<id> is the output id given to girafeOutput().

2.1 Geometries

Each geometry of ggplot2 has an interactive version: geom_point_interactive(), geom_line_interactive(), geom_sf_interactive()… and so do a few geometries of other packages: geom_text_repel_interactive() and geom_label_repel_interactive() (ggrepel), geom_quasirandom_interactive() (ggbeeswarm). annotate_interactive() adds an interactive annotation.

The interactive aesthetics can be mapped in aes(), or given as fixed values, as colour = "red":

mtcars_db <- mtcars
mtcars_db$carname <- row.names(mtcars_db)

gg_scatter <- ggplot(mtcars_db, aes(x = wt, y = mpg)) +
  geom_point_interactive(
    aes(tooltip = carname, data_id = carname),
    size = 3, hover_nearest = TRUE
  ) +
  geom_hline_interactive(
    yintercept = mean(mtcars_db$mpg),
    tooltip = "average consumption", data_id = "mean",
    linetype = "dashed"
  )

girafe(ggobj = gg_scatter)

Try it: hover the dashed line, it turns orange and the tooltip says “average consumption”; the points react as in the previous chapter.

2.2 Legends

The legends are made interactive by the scale: use the interactive version of the scale (scale_fill_manual_interactive(), scale_colour_viridis_c_interactive(), scale_linewidth_interactive()…) and give it the interactive aesthetics. Each one can be:

  • a function of the breaks, that returns one value per key; it receives the breaks as labels does: dates on a date scale, the original values (100, not 2) on a log scale;
  • a vector, with one value per key; named, its names are matched with the breaks.
bars <- ggplot(
  data = diamonds,
  mapping = aes(x = color, fill = cut, data_id = cut)
) +
  geom_bar_interactive(
    aes(tooltip = sprintf("%s: %.0f", fill, after_stat(count)))
  )

gg_bar <- bars +
  scale_fill_viridis_d_interactive(
    data_id = function(breaks) breaks,
    tooltip = function(breaks) paste("cut:", breaks)
  )

girafe(ggobj = gg_bar)

Try it: hover a key of the legend, it turns orange and the tooltip gives its cut; the bars do not react, the legend and the panel are not linked yet (see below).

The four guides of ggplot2 are supported: guide_legend_interactive(), guide_bins_interactive(), guide_colourbar_interactive() and guide_coloursteps_interactive(). The guide is chosen as usual, with the guide argument of the scale; the interactive version is used for you.

  • With the bins and coloursteps guides, the keys are classes: give one value per class, or one per break (limits included), each class taking the value of its lower bound.
  • A colour bar is drawn as a single image: it takes a single value.

The title and the labels of a legend are made interactive with label_interactive():

gg_bar_title <- bars +
  scale_fill_viridis_d_interactive(
    data_id = function(breaks) breaks,
    tooltip = function(breaks) paste("cut:", breaks),
    name = label_interactive(
      "Cut", data_id = "cut_title",
      tooltip = "Quality of the cut"
    ),
    labels = function(breaks) {
      label_interactive(breaks, data_id = breaks, tooltip = breaks)
    }
  )

girafe(ggobj = gg_bar_title)

Try it: hover the title of the legend, the tooltip says “Quality of the cut”; hover a label, the label and its key turn orange together.

2.3 Facet strips

With facet_wrap_interactive() or facet_grid_interactive(), the labeller labeller_interactive() gives the interactive aesthetics to the strips. Its aes() can use the faceting variables. The argument interactive_on sets what reacts: the text ("text", the default), the background ("rect") or both ("both").

gg_facets <- bars +
  facet_wrap_interactive(
    vars(clarity), ncol = 2,
    interactive_on = "both",
    labeller = labeller_interactive(aes(
      tooltip = paste("clarity:", clarity), data_id = clarity
    ))
  )

girafe(ggobj = gg_facets)

Try it: hover a strip, its text and its background turn orange and the tooltip gives the clarity.

The strips share the options and the identifiers of the geometries: they use opts_hover() and opts_selection(), and a strip is highlighted with the geometries that have the same data_id. In a Shiny application, a click on a strip can select the subset of the data to work on (input$<id>_selected).

2.4 Titles and theme elements

The theme elements become interactive with element_text_interactive(), element_rect_interactive() and element_line_interactive(), used in theme() in place of element_text(), element_rect() and element_line(). They accept the interactive aesthetics as arguments.

For the titles, label_interactive() gives the interactive aesthetics to the text, in labs(). The theme element of the title must be interactive too: without element_text_interactive(), the title is drawn but does not react.

gg_titles <- gg_scatter +
  labs(
    title = label_interactive(
      "Weight and consumption",
      data_id = "title",
      tooltip = "Data from the 1974 Motor Trend US magazine"
    ),
    x = label_interactive(
      "Weight", data_id = "wt", tooltip = "in 1000 lbs"
    )
  ) +
  theme(
    plot.title = element_text_interactive(),
    axis.title.x = element_text_interactive(),
    axis.title.y = element_text_interactive(
      data_id = "mpg", tooltip = "Miles per US gallon"
    )
  )

girafe(ggobj = gg_titles)

Try it: hover the title, the x axis title or the y axis title: each one turns orange and shows its own tooltip.

2.5 Linking elements with data_id

The elements that share a data_id are highlighted together: in a layer, in several layers, and in several plots assembled into one graphic.

2.5.1 Across layers

Here the line and the end point of each species share the name of the species as data_id: hovering one highlights the other.

dat <- readRDS("data/species-ts.RDS")
last_points <- dat[dat$date == max(dat$date), ]

gg_lines <- ggplot(
  dat,
  aes(x = date, y = score, colour = species, data_id = species, tooltip = species)
) +
  geom_line_interactive() +
  geom_point_interactive(data = last_points, size = 3) +
  scale_colour_viridis_d() +
  theme(legend.position = "none")

girafe(
  ggobj = gg_lines, width_svg = 8, height_svg = 5,
  options = list(opts_hover_inv(css = "opacity:0.1;"))
)

Try it: hover a line or the point at its end, both turn orange and the other species almost disappear.

2.5.2 Between the legend and the geometries

The legend keys and the geometries are separate parts of the graphic: by default, hovering a key only highlights the key. With linked = TRUE in opts_hover(), a key and the geometries with the same data_id are highlighted together, from the legend or from the panel. The data_id of the keys must be the values of the data_id of the geometries.

girafe(
  ggobj = gg_bar,
  options = list(
    opts_hover(css = "fill:black;", linked = TRUE),
    opts_hover_key(css = "fill:black;"),
    opts_hover_inv(css = "opacity:0.5;")
  )
)

Try it: hover a key of the legend, it turns black with all the bars of its cut, and the other bars fade; hover a bar, its key turns black too.

opts_selection(linked = TRUE) does the same for the selections, see Shiny.

2.5.3 Across plots

Plots assembled with patchwork (or cowplot) form one graphic: the elements with the same data_id are highlighted together, from one plot to the other.

gg1 <- ggplot(mtcars_db) +
  geom_point_interactive(
    aes(x = drat, y = wt, color = qsec, tooltip = carname, data_id = carname),
    hover_nearest = TRUE, size = 3
  )

gg2 <- ggplot(mtcars_db) +
  geom_point_interactive(
    aes(x = qsec, y = disp, color = mpg, tooltip = carname, data_id = carname),
    hover_nearest = TRUE, size = 3
  )

girafe(ggobj = gg1 + gg2)

Try it: hover a point in one plot, the same car turns orange in the other plot.

The legend keys (without linked = TRUE), the titles and the theme elements have their own identifiers: they are not linked to the geometries.