How to Plot With Ggiraph: Exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


The ggiraph is an htmlwidget and a ggplot2 extension. It allows ggplot graphics to be animated.

Animation is made with ggplot geometries that can understand three arguments:

Tooltip: a column of data-sets that contain tooltips to be displayed when the mouse is over elements.
Onclick: a column of data-sets that contain a JavaScript function to be executed when elements are clicked.
Data_id: a column of data-sets that contain an ID to be associated with elements.

If it used within a shiny application, elements associated with an ID (data_id) can be selected and manipulated on client and server sides.

Before proceeding, please follow our short tutorial.

Look at the examples given and try to understand the logic behind them. Then, try to solve the exercises below by using R without looking at the answers. Then, check the solutions to check your answers.

Exercise 1

Create a basic ggplot object of the mtcars data-set, choosing variables of your choice.

Exercise 2

Create a scatter-plot of the object you just created by adding a tooltip.

Exercise 3

Add the hover effect.

Exercise 4

Create a crimes data frame.

Exercise 5

Create an on-click event.

Exercise 6

Add the on-click event. Connect every state with the link

Exercise 7

Change the http to

Exercise 8

Convert gg_crime <- ggplot(crimes, aes(x = Murder, y = Assault, color = UrbanPop )) +


aes( data_id = state, tooltip = state ), size = 3 ) +

scale_colour_gradient(low = “#999999”, high = “#FF3333”)

to an on-click plot.

Exercise 9

Display the plot.

Exercise 10

Change the spots’ color to orange.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)