How to Plot With Ggiraph: Exercises
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
INTRODUCTION
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 http://en.wikipedia.org/wiki/.
Exercise 7
Change the http to https://en.wikipedia.org/wiki/U.S._state.
Exercise 8
Convert gg_crime <- ggplot(crimes, aes(x = Murder, y = Assault, color = UrbanPop )) +
geom_point_interactive(
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.
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.