Custom Interactivity: Elegant Interactive Charts with ggiraph
Standard interactive options can be frustrating because they are just a wrapper around a JS library and may break your custom design choices. The ggiraph package solves this by matching the native grammar of graphics, letting you turn static visualizations into interactive plots while keeping your styling intact.
In this lesson, we'll explore how to bring your static charts to life with ggiraph. We cover the core workflow: replacing traditional geoms with interactive geometries and exploring how to map data variables to tooltips and basic hover states.
Before You Start π§°
Weβre using custom fonts in this lesson to showcase non-default styling. If you'd like to run the examples on your machine, make sure to install Rethink Sans and Domine on your system if you haven't yet.
π₯ Easy to Make (it Break)
When it comes to interactive graphics in R, most people will immediately mention the plotly package. This is largely because it was the first one-size-fits-all solution to create interactive versions of almost any ggplot2 chart β and because it feels incredibly easy to use.
The catch? It feels easy. But it isn't very flexible. The moment you want to customize your interactive behavior, you find yourself wading deep into JavaScript adjustments where it's incredibly easy to get stuck.
You might now argue: "But creating a default graphic with plotly is super-easy" β and it definitely is: one can use the beloved ggplot syntax and it's a one-liner to turn it into an interactive plot.
But here is what makes it so frustrating: the resulting graphic doesn't look or feel like a ggplot anymore. Detailed changes to your theme? Just gone. Nice axis formatting? Legend styling? All gone. Labeling with ggrepel? Not working anymore. And so on and so forth.
In the meantime, an elegant alternative one-size-fits-all solution has entered the scene: the ggiraph package.
And now, every time I hear someone say, "plotly is the go-to approach and it's so simple", I just smile and respond "ggiraph is simple yet beautiful".
π― Interactive Geometries
The ggiraph package takes a different approach thanplotly. Instead of wrapping an entire ggplot, it replaces individual geoms with interactive versions. This gives you precise control over which elements are interactive and how they behave.
This means all you have to do is add an _interactive suffix to your geoms, and you're good to go! The rest of your ggplot code remains unchanged.
Doors are closed πͺ
Enrollment for the current cohort is closed. Join the waitlist to be notified as soon as the next cohort opens, and become the ggplot2 expert your company needs!
Already enrolled? Login