Skip to contents

The ggiraph package allows us to easily turn ggplots into interactive plots, while retaining the style we’ve created.

The basic idea

All we have changed from the plot in the opening README file is replace geom_point() with ggiraph::geom_point_interactive(), and the addition of a tooltip argument to aes().

library(tidyverse)

penguin_plot <- palmerpenguins::penguins %>%
  ggplot() +
  ggiraph::geom_point_interactive(aes(x = bill_length_mm,
                 y = flipper_length_mm,
                 fill = body_mass_g,
                 size = body_mass_g,
                 tooltip = paste0("Body mass<br><b>", sprintf("%.03f", body_mass_g/1000), "kg</b>")),
             shape = 21,
             colour = "white",
             alpha = 0.8,
             show.legend = FALSE) +
  labs(x = "Bill length (mm)",
       y = "Flipper length (mm)",
       title = "Perfectly proportional penguins",
       caption = "Data from {palmerpenguins}") +
  ophelia::scale_fill_ophelia(continuous = TRUE) +
  ophelia::theme_ophelia()

ggiraph::girafe(ggobj = penguin_plot)

Adding the Ophelia style to the tooltips

To easily format the tooltips using the Ophelia aesthetic, I have added the required CSS string to the package.

ophelia::tooltip_css
#> [1] "background-color:#2B2529;color:#FEFCF7;padding:5px;border-radius:5px;font-family:Nunito Sans;"

We can use it within the girafe code as follows:

ggiraph::girafe(ggobj = penguin_plot, 
                   options = list(ggiraph::opts_tooltip(css = ophelia::tooltip_css)))