Tidyverse Book

NIcholas Schettini

May 4, 2018

Within the Tidyverse is the function ggplot2. Ggplot2 allows one to create visually pleasing graphics, including scatterplots, boxplots, density plots and more. There is a complement to the ggplot/tidyverse universe. It’s called ggiraph. Ggiraph allows the user to turn their normal ggplot graphs into interactive graphics.

Some usage of the ggiraph functions are:

  • geom_bar_interactive
  • geom_boxplot_interactive
  • geom_histogram_interactive
  • geom_line_interactive
  • geom_map_interactive
  • geom_path_interactive
  • geom_point_interactive
  • geom_polygon_interactive
  • geom_rect_interactive
  • geom_segment_interactive
  • geom_text_interactive
  • geom_tile_interactive

The ggiraph package lets you add three more aesthetics to your ggplot graph.

  • tooltip: column of dataset that contains tooltips to be displayed when mouse is over elements.
  • data_id: column of dataset that contains id to be associated with elements

The ggiraph function is called using library(ggiraph).

Tooltips Tooltips can be created using the following example:

library(tidyverse)
library(ggiraph)
library(fueleconomy)
ggexample <- ggplot(vehicles, aes(factor(vehicles$year), vehicles$hwy)) +
  geom_boxplot_interactive(aes(tooltip = year))


ggiraph(code = print(ggexample))
## Warning: package 'gdtools' was built under R version 3.4.4

Hover effects

ggexample <- ggplot(vehicles, aes(factor(vehicles$year), vehicles$hwy, color = hwy)) +
  geom_boxplot_interactive(aes(tooltip = year, data_id = year))


ggiraph(code = print(ggexample))

As you can see in the example above, the boxs change color on hover.