2025-07-08
Hover over the data points
Hover over the data points
Hover over the data points
Hover over the data points
Click on one of the states
Regular ggplot2 plot:
hovering does nothing
ggiraph version:
hovering displays species names
library(ggiraph)
iris_scatter <- ggplot(iris) + 
  aes(
    Sepal.Length, Sepal.Width,
    color = Species
  ) +
  geom_point_interactive(
    aes(tooltip = Species)
  )
girafe(
  ggobj = iris_scatter,
  width_svg = 6,
  height_svg = 6*0.618,
  options = list(
    opts_tooltip(
      css = "background: #F5F5F5;
             color: #191970;
             font-family: sans-serif;"
    )
  )
)data_id aestheticdata_id aestheticlibrary(ggiraph)
iris_scatter <- ggplot(iris) + 
  aes(
    Sepal.Length, Sepal.Width,
    color = Species
  ) +
  geom_point_interactive(
    aes(data_id = Species),
    size = 2
  )
girafe(
  ggobj = iris_scatter,
  width_svg = 6,
  height_svg = 6*0.618,
  options = list(
    opts_hover(css = "fill: #202020;"),
    opts_hover_inv(css = "opacity: 0.2;")
  )
) Again, styling via CSS
# load data
US_states <- readRDS(url("https://wilkelab.org/DSC385/datasets/US_states.rds"))
US_statesSimple feature collection with 51 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -3683715 ymin: -2839538 xmax: 2258154 ymax: 1558935
CRS:           NA
First 10 features:
   GEOID                 name state_code                       geometry
1     01              Alabama         AL MULTIPOLYGON (((1032679 -63...
2     04              Arizona         AZ MULTIPOLYGON (((-1216674 -4...
3     05             Arkansas         AR MULTIPOLYGON (((462619.4 -3...
4     06           California         CA MULTIPOLYGON (((-2077630 -2...
5     08             Colorado         CO MULTIPOLYGON (((-527710.6 3...
6     09          Connecticut         CT MULTIPOLYGON (((1841099 622...
7     10             Delaware         DE MULTIPOLYGON (((1762798 354...
8     11 District of Columbia         DC MULTIPOLYGON (((1610777 321...
9     12              Florida         FL MULTIPOLYGON (((1431218 -13...
10    13              Georgia         GA MULTIPOLYGON (((1339965 -64...Hover over the states
US_map <- US_states |>
  mutate( # JavaScript call to open website 
    onclick = glue::glue(
'window.open(
"https://en.wikipedia.org/wiki/{name}")')
  ) |>
  ggplot() +
  geom_sf_interactive(
    aes(
      data_id = name, tooltip = name,
      onclick = onclick
    )
  ) +
  theme_void()
girafe(
  ggobj = US_map,
  width_svg = 6,
  height_svg = 6*0.618
)Click to open a state’s wikipedia page
CSS is the language used to style web pages
Many online tutorials, e.g.: https://www.w3schools.com/css/default.asp
Interactive websites for practicing, e.g.: https://jsfiddle.net/