Make ggplot graphics2 interactive with ggiraph
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
R's ggplot2 package is a well-known tool for producing beautiful static data visualizations that you can include in a printed report. But what if you want to include a ggplot2 graphic on a webpage and provide the ability for the user to interact with the data? The ggiraph package by David Gohel (available for installation via CRAN). WIth ggiraph, you can take an existing ggplot2 bar chart, scatterplot, boxplot, map, or many other types of chart and add one or both of the following iteractions:
- Display a tooltip of your choice (e.g. data values or labels) when the cursor hovers over sections of the chart
- Perform an action (a javascript function you provide: jump to another page, for example) when the viewer clicks on an element of the chart
The tweet below (from Duc Quang Nguyen, a data journalist at swissinfo.ch) shows an animation of ggiraph in action: a map of the net migration rate into (green) or out of (brown) regions in Europe.
Les profils de l'immigration diffèrent largement au sein des pays européens. Décryptage en graphiques https://t.co/CYby19IcBS #dataviz #ddj pic.twitter.com/F9RhWDezdD
— Duc Quang Nguyen (@duc_qn) October 6, 2016
You can try out other examples of interactive charts in this ggirpah vignette. If you already have chart written in ggplot2, it's easy to make interactive: just replace the existing geom call (e.g. geom_map) with its ggiraph equivalent (geom_map_interactive) , and provide extra arguments with information about the labels and/or javascript actions. For more details on using ggiraph, check out its home on github, linked below.
Github (davidgohel): ggiraph
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.