ggiraph: interactive ggplot2 graphics
Preface
The ‘ggiraph’ package lets you create dynamic and interactive graphics: dynamic with animations and tooltips, interactive in shiny applications where events/interactions with ggiraph graphic are made available as reactive values.
Under the hood, ‘ggiraph’ is an ‘htmlwidget’ and a ‘ggplot2’ extension. It allows graphics to be interactive, by exporting them as SVG documents and using special attributes on the various elements. The package also contains an R graphics device dedicated to SVG: it is used to produce ‘ggiraph’ graphics, and can also produce standard SVG graphics.
Why use ‘ggiraph’
- You want to provide your readers with more information than the basic information available; you can display a tooltip when the user’s mouse is on a graphical element, you can also visually animate elements with the same attribute when the mouse passes over a graphical element, and finally you can link a JavaScript action to the click, such as opening a hypertext link.
- You want to allow users of a Shiny application to select graphical elements; for example, you can make the points of a scatter plot selectable and available as a reactive value from the server part of your application. With Shiny, ‘ggiraph’ allows interaction with graph elements, legends elements, titles and ggplot theme elements from the server part; each selection is available as a reactive value.
How does it compare to plotly
A common question is how ggiraph compares to plotly::ggplotly(). Both add interactivity to ggplot2 graphics, but with different approaches:
- ggplot2 fidelity: ggiraph produces SVG with an actual R graphics device (the same rendering pipeline as
pdf()orpng()). The output is pixel-identical to your static ggplot.ggplotly()re-interprets the ggplot object in JavaScript, which can lead to visual differences, some geoms, themes or extensions may not translate correctly. - Learning curve:
ggplotly()works instantly, wrap any existing ggplot and you get tooltips, zoom and pan with no extra code. ggiraph requires the user to explicitly specify interactive aesthetics (tooltip,data_id,onclick), which takes more effort up front but gives full control over what is interactive and how. - Interactivity model: ggiraph provides tooltip, hover animation, element selection (single/multiple/lasso) and onclick actions. In Shiny, selections and hover events are available as reactive values for all parts of the graphic (panel, legend, theme).
plotlyprovides built-in zoom, pan, hover and click events. - Selection as a basket: with ggiraph, users can build a selection incrementally, clicking elements adds or removes them from a basket (single or multiple mode), and lasso selection lets them draw a region to select/unselect many elements at once. The current selection is always available as a Shiny reactive value. This basket-like behavior is central to ggiraph and has no direct equivalent in
plotly. - Shiny integration: ggiraph is designed around Shiny, element selections via click or lasso, programmatic CSS class manipulation, and linked legend/geometry interactions are first-class features.
plotlyoffers Shiny integration throughplotly::event_data().
In short, ggplotly() is the quickest path to an interactive graphic. ggiraph requires more explicit setup but gives you pixel-perfect ggplot2 rendering, a powerful selection model for Shiny, and full control over every interactive behavior.
How this book is organized
- Getting started: the three interactive aesthetics and a first interactive graphic.
- What can be interactive: geometries, legends, facet strips, titles and theme elements, and how a shared
data_idlinks them. - Options: tooltips, hover effects, zoom, toolbar and size.
- Shiny: selections and hover events as reactive values.
- Fonts: the fonts used to draw the graphic and to show it in the page.
- Examples and the FAQ.