Easily create interactive ggplot graphs in R with ggiraph

Static visualizations are frequently more than enough to tell stories with your knowledge. But at

Static visualizations are frequently more than enough to tell stories with your knowledge. But at times you want to increase interactivity, so consumers can hover about graphs to see underlying knowledge or connection their hover about a person visualization to highlighting knowledge in another. 

R has a range of packages for producing interactive graphics which include echarts4r, plotly, and highcharter. I like and use all of individuals. But for easy linking of interactive graphs, it is hard to defeat ggiraph.

From ggplot to ggiraph in 3 easy techniques

There are a few easy techniques to transform ggplot code into an interactive graph:

  1. Use a ggiraph interactive geom as an alternative of a “regular” ggplot geom. The structure is easy to remember: Just increase _interactive to your regular geom. So, geom_col() for a typical bar chart would be geom_col_interactive(), geom_stage() would be geom_stage_interactive(), and so on. 
  2. Include at minimum a person interactive argument to the graph’s aes() mapping: tooltip, knowledge_id, or onclick. That knowledge_id argument is what connects two graphics, letting you hover about a person and impact the display screen of another a person — all without having Shiny. 
  3. Immediately after producing your ggiraph dataviz object, use the girafe() functionality to transform it into a JavaScript graphic. Certainly, which is girafe() like the animal but with a person f. (That is how you spell it in French, and the creator of ggiraph, David Gohel, life in Paris.)

Put in the R packages

If you’d like to follow together with the code in this tutorial, you will will need the ggplot2, ggiraph, dplyr, and patchwork packages from CRAN on your program as very well as ggiraph. And, to build a map, I’ll be using Bob Rudis’s albersusa offer, which isn’t on CRAN. You can install it from GitHub with 




Get ready the knowledge

For knowledge, I’m likely to use modern US Covid vaccination knowledge by state available from the Our Earth in Information GitHub repository.

In the code below, I’m loading libraries, reading through in the vaccination knowledge, and changing “New York State” to just “New York” in the knowledge frame.


knowledge_url <- "https://github.com/owid/covid-19-data/raw/master/public/data/vaccinations/us_state_vaccinations.csv"

all_knowledge <- read.csv(data_url)
all_knowledge$location[all_knowledge$location == "New York Condition"] <- "New York"

Next, I build a vector of entries that are not US states or DC. I’ll use it to filter out that knowledge so my chart doesn’t have way too numerous rows. 

not_states_or_dc <- c("American Samoa", "Bureau of Prisons", 
"Dept of Protection", "Federated States of Micronesia", "Guam",
"Indian Well being Svc", "Lengthy Term Care", "Marshall Islands",
"Northern Mariana Islands", "Puerto Rico", "Republic of Palau",
"United States", "Veterans Well being", "Virgin Islands")

This up coming code block filters out the non_states_or_dc rows, chooses only the most modern knowledge, rounds the {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa} vaccinated to a person decimal stage, selects only the state and {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa} vaccinated columns, and renames my chosen columns to Condition and PctFullyVaccinated.

bar_graph_knowledge_modern <- all_data {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}  
filter(date == max(date), !(location {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}in{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa} not_states_or_dc)) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
PctFullyVaccinated = spherical(individuals_fully_vaccinated_for every_hundred, 1)
) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
find(Condition = location, PctFullyVaccinated)

Produce a fundamental bar graph with ggplot2

Next I’ll build a fundamental (static) ggplot bar chart of the knowledge. I use geom_col() for a bar chart, increase my personal customary blue bars outlined in black and small concept, set the axis text dimension to ten factors, and flip the x and y coordinates so it is a lot easier to read through the state names. 

bar_graph <- ggplot(bar_graph_data_recent, 
aes(x = reorder(Condition, PctFullyVaccinated),
y = PctFullyVaccinated)) +
geom_col(coloration = "black", fill="#0072B2", dimension = .5) +
concept_small() +
concept(axis.text=component_text(dimension = ten)) +
labs(title = "{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa} Absolutely Vaccinated July 2021",
subtitle = "Information from Our Earth in Information GitHub repo"
) +
ylab("") +
xlab("") +


Bar chart of state vaccination data Sharon Machlis, IDG

Bar chart of US vaccination knowledge by state designed with ggplot2. Information from Our Earth in Information.

Produce a tooltip column in R

ggiraph only lets me use a person column for the tooltip display screen, but I want both of those state and rate in my tooltip. There is an easy option: Include a tooltip column to the knowledge frame with both of those state and rate in a person text string:

bar_graph_knowledge_modern <- bar_graph_data_recent {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
tooltip_text = paste0(toupper(Condition), "n",
PctFullyVaccinated, "{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}")

Make the bar chart interactive with ggiraph

To build a ggiraph interactive bar chart, I changed geom_col() to geom_col_interactive() and added tooltip and knowledge_id to the aes() mapping. I also diminished the dimension of the axis text, for the reason that the ggplot dimension ended up currently being way too significant.

Then I shown the interactive graph object with the girafe() functionality. You can set the graph width and peak with width_svg and peak_svg arguments within girafe()

most up-to-date_vax_graph <- ggplot(bar_graph_data_recent, 
aes(x = reorder(Condition, PctFullyVaccinated),
y = PctFullyVaccinated,
tooltip = tooltip_text, knowledge_id = Condition #<<
)) +
geom_col_interactive(coloration = "black", fill="#0072B2", dimension = .5) + #<<
concept_small() +
concept(axis.text=component_text(dimension = six)) + #<<
labs(title = "{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa} Absolutely Vaccinated July 2021",
subtitle = "Information from Our Earth in Information GitHub repo"
) +
ylab("") +
xlab("") +

girafe(ggobj = most up-to-date_vax_graph, width_svg = 5, peak_svg = four)

The graph will glance pretty identical to the ggplot edition — but if you operate the code yourself or view the video clip embedded higher than, you will see that you can now hover about the bars and see underlying knowledge. 

Bar chart with blue bars and one yellow bar showing a small box with state name and vaccination rate Sharon Machlis, IDG

If you hover about a bar on a ggiraph graph, the bar is highlighted and you can see a tooltip with underlying knowledge. Information from Our Earth in Information.

A single thing that actually helps make ggiraph glow is how easy it is to connection up a number of graphs. To demo that, of training course, I’ll will need a 2nd visualization to connection to my bar chart.

The code below generates a knowledge frame with vaccination knowledge from February fourteen, 2021, and a ggiraph bar chart with that knowledge.

bar_graph_knowledge_early <- all_data {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
filter(date == "2021-02-fourteen", !(location {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}in{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa} not_states_or_dc)) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
prepare(individuals_fully_vaccinated_for every_hundred) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
PctFullyVaccinated = spherical(individuals_fully_vaccinated_for every_hundred, 1),
tooltip_text = paste0(toupper(location), "n", PctFullyVaccinated, "{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}")
) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
find(Condition = location, PctFullyVaccinated, tooltip_text)

early_vax_graph <- ggplot(bar_graph_data_early, aes(x = reorder(State, PctFullyVaccinated), y = PctFullyVaccinated, tooltip = tooltip_text, data_id = State)) +
geom_col_interactive(coloration = "black", fill="#0072B2", dimension = .5) +
concept_small() +
concept(axis.text=component_text(dimension = six)) +
labs(title = "Absolutely Vaccinated as of February fourteen, 2021",
subtitle = "Information from Our Earth in Information"
) +
ylab("") +
xlab("") +

Hyperlink interactive graphs with ggiraph

The code to connection the two graphs is really easy. Under I use the girafe() functionality to say I want to print the early_vax_graph in addition the latest_vax_graph and set the canvas width and peak. I also increase an solution so when the user hovers, the bars transform cyan.

girafe(code = print(early_vax_graph + most up-to-date_vax_graph), 
width_svg = eight, peak_svg = four) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
girafe_selections(opts_hover(css = "fill:cyan"))

Linking the two graphs helps make it easy for consumers to see what took place to state rankings in between February and July. For illustration, by hovering about Alaska in the February graph, the bar for Alaska in the July graph also turns cyan. (Devoid of that solution, the bars would transform the default yellow coloration.)

2 bar charts side by side with bars for Alaska highlighted in both. Sharon Machlis, IDG

Hovering about a state’s bar in a person graph highlights that state’s bars on both of those graphs. Information from Our Earth in Information.

Hyperlink a map and bar chart with ggiraph

This plan arrives from Kyle E. Walker, who coded a demo employing his tidycensus offer to build a map connected with a chart. We can do the identical with this knowledge and a map from scratch employing the albersusa offer (even though I extremely recommend tidycensus if you are doing the job with U.S. Census knowledge).

Under is the code for the map. us_sf is an R easy features geospatial object designed with the albersusa::united states of america_sf() functionality. state_map creates a ggiraph map object from that us_sf object. The map code takes advantage of common ggplot() syntax, but as an alternative of geom_sf() it takes advantage of geom_sf_interactive(). There are also tooltip and knowledge_id arguments in the aes() mapping. Eventually, the code eradicates any history or axes with concept_void().

us_sf <- usa_sf("lcc") {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
mutate(Condition = as.character(title))

state_map <- ggplot() +
geom_sf_interactive(knowledge = us_sf, dimension = .one hundred twenty five,
aes(knowledge_id = Condition, tooltip = Condition)) +

The up coming code block takes advantage of girafe() and its ggobj argument to display screen both of those the map and the vax graph, connected interactively.

girafe(ggobj = state_map + most up-to-date_vax_graph, 
width_svg = ten, peak_svg = 5) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
girafe_selections(opts_hover(css = "fill:cyan"))

Now if I hover about a state on the map, its bar “lights up” on the bar chart.

Map next to a bar chart. New York State is highlighted on both the map and bar chart. Sharon Machlis, IDG

Hover about a state on the map, and its corresponding bar “lights up” on the bar chart. Information from Our Earth in Information.

It can take pretty very little R code to make a static graphic interactive and to connection two graphs jointly.

How to use your ggiraph knowledge visualizations

You can increase ggiraph visualizations to an R Markdown doc and create an HTML file that functions in any website browser.

You can also save output from the girafe() functionality as an HTML widget and then save the widget to an HTML file employing the htmlwidgets offer. For illustration:  

my_widget <- girafe(ggobj = state_map + latest_vax_graph, 
width_svg = ten, peak_svg = 5) {446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}>{446c0583c78045abf10327776a038b2df71144067b85dd55dd4a3a861892e4fa}
girafe_selections(opts_hover(css = "fill:cyan"))

htmlwidgets::saveWidget(my_widget, "my_widget_website page.html",
selfcontained = True)

For a lot more on ggiraph, test out the ggiraph offer internet site.

And for a lot more R suggestions, head to the InfoWorld Do Far more With R website page.

Copyright © 2021 IDG Communications, Inc.