Skip to main content

ggiraph in one page

A dense summary of the rules and of the API, to keep at hand. Each point links to the chapter that explains it.

The workflow

  1. Build a ggplot with interactive layers (geom_*_interactive()) and, if needed, interactive scales, facets and theme elements.
  2. Give it to girafe(ggobj = gg), with options in options = list(opts_*()).
  3. In Shiny: girafeOutput("plot") in the UI, renderGirafe(girafe(...)) in the server.

Without girafe(), the plot is an ordinary ggplot.

The rules

  • No data_id, no hover effect and no selection. A tooltip alone only shows a tooltip. A missing (NA) value gives nothing: the element does not react.
  • The elements that share a data_id are highlighted and selected together, across layers and across plots assembled with patchwork.
  • A legend key and the geometries with the same data_id are linked only with opts_hover(linked = TRUE) or opts_selection(linked = TRUE).
  • A title made with label_interactive() reacts only if its theme element is interactive: theme(plot.title = element_text_interactive()).
  • Selections are off outside Shiny: use opts_selection(only_shiny = FALSE) to try them in a document.
  • Hover events are Shiny inputs only with opts_hover(reactive = TRUE).
  • Interactive aesthetics can change the groups of ggplot2 (dodging, reordering): set group explicitly (FAQ).
  • Fonts: the generic families ("sans", "serif", "mono") use girafe(font_set = ), by default the Liberation fonts. A font named in the plot must be registered and added to the page (Fonts).

Interactive parameters

Mapped in aes() or given as fixed arguments of the layer:

Parameter Effect
tooltip text or HTML (htmltools::HTML()) shown on hover
data_id identifier: hover, selection, Shiny values, links between elements
onclick JavaScript code run on click
hover_css, selected_css css of this element when hovered or selected, overriding the options
tooltip_fill tooltip background with opts_tooltip(use_fill = TRUE)
hover_nearest TRUE: hover the element nearest to the mouse (needs data_id)

The parts of a graphic

Part Made interactive with Hover Selection Shiny input
geometries, facet strips geom_*_interactive(), facet_wrap_interactive() / facet_grid_interactive() with labeller_interactive() opts_hover() opts_selection() input$<id>_selected
legends scale_*_interactive(); guides guide_legend_interactive(), guide_bins_interactive(), guide_colourbar_interactive(), guide_coloursteps_interactive() opts_hover_key() opts_selection_key() input$<id>_key_selected
titles, theme elements label_interactive(), element_text_interactive(), element_rect_interactive(), element_line_interactive() opts_hover_theme() opts_selection_theme() input$<id>_theme_selected

Legend values given to a scale: a function of the breaks, or a vector (one value per key; named, matched with the breaks). Binned guides: one value per class or per break. Colour bar: a single value. See What can be interactive.

Options

Function Sets
opts_tooltip() tooltip css, offset (offx, offy), use_fill, use_stroke, use_cursor_pos
opts_hover() hover css, reactive, linked, nearest_distance
opts_hover_inv() css of the elements not hovered, e.g. "opacity:0.2;"
opts_hover_key(), opts_hover_theme() hover css of the legends and of the theme elements, reactive
opts_selection() type ("multiple", "single", "none"), css, selected, only_shiny, linked
opts_selection_inv() css of the elements not selected
opts_selection_key(), opts_selection_theme() selection of the legends and of the theme elements
opts_zoom() zoom: max > 1 enables it, default_on
opts_toolbar() position, fixed, hidden, saveaspng, pngname, pngscale (size of the PNG, 2 by default), colors
opts_sizing() rescale (responsive or fixed size), width
  • girafe_css() gives a css per kind of SVG element (text, point, line, area, image); girafe_css_bicolor() builds a two-color set. The lines are never filled, unless line asks for it.
  • set_girafe_defaults() sets the options of all the graphics of the session; girafe_defaults() reads them; init_girafe_defaults() restores them. The options of set_girafe_defaults(), girafe(options = ) and girafe_options() are combined argument by argument.

See Options.

Size

width_svg and height_svg (inches) set the aspect ratio and the resolution; opts_sizing() and girafeOutput(width, height) set the size in the page. In R Markdown and Quarto, fig.width and fig.height are used.

Shiny

Name Content
input$<id>_selected, input$<id>_key_selected, input$<id>_theme_selected data_id of the selected elements
input$<id>_hovered, input$<id>_key_hovered, input$<id>_theme_hovered data_id of the hovered elements (with reactive = TRUE)
  • Change a selection from the server: session$sendCustomMessage(type = "<id>_set", message = ids), also <id>_key_set, <id>_theme_set; character(0) clears it. The hover states use <id>_hovered_set, <id>_key_hovered_set and <id>_theme_hovered_set.
  • Add CSS classes from the server: girafe_class_add(), girafe_class_remove(), girafe_class_toggle().
  • Example applications: run_girafe_example("crimes") and others.

See Shiny.