create interactive ggplot graphics with the ggiraph R package deal

Hello, I’m Sharon Machlis at IDG, right here with Episode 63 of Do Extra With R: Create interactive ggplot graphics with the ggiraph R package deal.
Static visualizations are sometimes sufficient to inform tales together with your information. However there are occasions you need interactivity, so customers can hover to see underlying or linked information.
R has quite a few packages for interactive graphics, together with echarts4r, plotly, and highcharter. I like and use all of these. However immediately, I’ll demo one other one which’s pretty new to me (and possibly to you): the ggiraph package deal – that’s g-g-i-r-a-p-h – to show ggplot graphics into interactive Net visualizations. I particularly like ggiraph for the way simple it’s to make a 2nd plot additionally react if you hover over a primary graphic. Let’s have a look.
There are three simple ggiraph steps to make your ggplots interactive:
1. Use a ggiraph interactive geom as a substitute of a “common” ggplot geom. The format is tremendous simple to recollect: Simply add underscore interactive to your typical geom. So, geom_col() for an everyday bar chart can be geom_col_underscore_interactive(). Geom_point() can be geom_point_underscore interactive(), and so forth.
2. Add a minimum of one interactive argument to the graph: tooltip, data_id, or onclick. You add these to a ggplot a-e-s aesthetics mapping. That data_id argument may be very helpful, as a result of it’s the one that allows you to hover over one graphic and have an effect on one other one – all with out Shiny. We’ll see that in a bit.
3. After creating your ggiraph dataviz object, you flip it into an precise Net graphic with the girafe() perform. The package deal is g-g-i-r-a-p-h, however the perform is spelled g-i-r-a-f-e (just like the animal, however with one f. That’s the way you spell it in French, and the creator of ggiraph, David Gohel, is in Paris.)
Let’s begin with a easy tooltip instance. When you’d wish to comply with alongside, these are the packages I’ll be utilizing: ggplot2, ggiraph, dplyr, patchwork, and for a map on the finish, Bob Rudis’s albersusa package deal. albersusa isn’t on CRAN; you’ll want to put in it from GitHub.
For information, I’m going to make use of latest US Covid vaccination information by state obtainable from the Our World in Knowledge GitHub repository. You may see the URL for the information. When you’re following alongside and don’t need to kind out that lengthy URL, go to bit-dot-l-y-slash-VaxData, capital V and D, which can redirect there.
Right here I’m studying within the information, and altering “New York State” to simply “New York”. That subsequent half creates a vector of entries that aren’t US states or DC, so my chart doesn’t have too many rows. You don’t have to do this, however it’s useful so the chart isn’t so giant.
This code filters the information to create an information set I can use to visualise the newest information for every state and DC, excluding the others. I’m additionally rounding the % vaccinated to 1 decimal level, deciding on simply the state and vax % columns, and renaming them to State and PctFullyVaccinated.
Right here’s a fundamental (static) ggplot bar chart of the information. X axis is initially the state, reordered primarily based on % vaccinated. y is the vax stage. I exploit geom_col() for a bar chart with my typical blue bars outlined in black, minimal theme, 10-point axis textual content. On the finish I flip the x and y coordinates so it’s simpler to learn the state names.
Now I need to flip this right into a Net graphic the place I can hover and see the underlying information. ggiraph solely lets me use one column for the tooltip information, however I need each state and fee in there. Straightforward answer: I add my very own tooltip column to the information body with each state and fee in there. I did that with the brand new tooltip_text column.
So my interactive latest_vax_graph code seems to be fairly much like my ggplot code. I added tooltip and data_id to the a.e.s mapping, and I modified geom_col() to geom_col_interactive(). I additionally lowered the dimensions of the axis labels, as a result of the ggplot 9 measurement ended up being too giant for my slide show.
Then on the code on the proper, I displayed the graph with the girafe perform. I set the ggobject to be my latest_vax_graph object, and I used to be additionally in a position to set the SVG width and top. The graph could seems to be just about the identical because the ggplot model – however now I can hover over the bars and see the underlying information.
One factor that basically makes ggiraph shine is how simple it’s to hyperlink up a number of graphs. Right here I’m creating information for a second graph for February 14 charges, after which I make one other ggiraph graph the identical means. However linking them is the place this will get particular.
Right here’s the code. I load the patchwork library, then use the girafe() perform to say I need to print the early_vax_graph plus the latest_vax_graph; and once I hover, I need the bars to show cyan colour. Right here’s what that appears like.
Now we will discover what occurred to state rankings between February and July. It’s that simple to hyperlink up 2 graphs!
I need to present you yet one more linking instance, this time with a map. I acquired the concept from Kyle Walker, who did a demo of utilizing his tidycensus package deal to create a map linked with a chart. I’ll use the identical thought with this information.
Right here’s the code. us_sf is a straightforward options geospatial object utilizing the “lcc” projection. state_map creates a ggiraph map object of the US from that. The map makes use of typical ggplot() syntax, however as a substitute of geom_sf() it’s geom_sf_interactive(). I’ve additionally added tooltip and data_id arguments to the aes() mapping, and I’ve eradicated any background or axes with theme_void()
The following code block makes use of girafe() to print each the map and the vax graph. There’s a barely completely different girafe() syntax right here, which Kyle Walker used: As a substitute of the code equal print argument I used earlier than, he used ggobject equals first viz plus second viz. Every thing else is identical. Let’s see what that appears like.
Now I can hover over a state on the map and see the place it exhibits up on the bar graph! Critically, how cool is that – with so little R code wanted!
That’s it for this episode, thanks for watching! For extra R suggestions, head to the Do Extra With R web page at bit-dot-l-y slash do extra with R, all lowercase aside from the R. It’s also possible to discover the Do Extra With R playlist on YouTube’s IDG Tech Speak channel the place you may subscribe so that you by no means miss an episode. Hope to see you subsequent time. Keep wholesome and secure, everybody!

Source link