The ggiraph package let R users make ggplot interactive. The package is an htmlwidget. The following graphic is produced by calling girafe() function with a ggplot object.

library(ggiraph)
library(ggplot2)

gg_point_1 <- ggplot(
  data = mtcars,
  mapping = aes(
    x = disp, y = qsec,
    color = wt, tooltip = row.names(mtcars), data_id = row.names(mtcars)
  )
) +
  geom_point_interactive(size = 2) +
  scale_color_gradient(low = "#F3C899", high = "#8C120A")

x <- girafe(
  ggobj = gg_point_1, width_svg = 6, height_svg = 6,
  options = list(
    opts_sizing(rescale = FALSE),
    opts_tooltip(
      opacity = .8,
      css = "background-color:gray;color:white;padding:2px;border-radius:2px;"
    ),
    opts_hover(css = "fill:#1279BF;stroke:#1279BF;cursor:pointer;")
  )
)
x

Introduction

It extends ggplot2 with:

These understand three aesthetics to let you add interactivity:

  • 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. This aesthetic is mandatory when you want to use an hover effect or when you want to enable selection of points in shiny applications.
  • onclick: column of dataset that contains javascript function to be executed when elements are clicked.

Let’s prepare a ggplot object with the mpg dataset.

library(ggplot2)
library(ggiraph)
g <- ggplot(mpg, aes( x = displ, y = cty, color = hwy) )

tooltips

The first example shows how to add a tooltip:

my_gg <- g + geom_point_interactive(aes(tooltip = model), size = 2) 
girafe(code = print(my_gg) )

hover effects

Now let’s add an hover effect. Elements associated with a data_id will be animated upon mouse over.

my_gg <- g + geom_point_interactive(
    aes(tooltip = model, data_id = model), size = 2) 
x <- girafe(code = print(my_gg))
x

Note that data-id can also be reused within a shiny application as a reactive value.

Click actions

Within shiny

When working with shiny, you can use the data_id aesthetic to associate points, polygons and other graphical elements with a value that will be available in a reactive context. This makes it possible to click on an element and trigger an action. Note that in this case, onclick should not be used, both onclick and data_id will need the “click” event. Read more about shiny integration

Using onclick

Click actions must be a string column in the dataset containing valid javascript instructions.

crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
head(crimes)
##                 state Murder Assault UrbanPop Rape
## Alabama       alabama   13.2     236       58 21.2
## Alaska         alaska   10.0     263       48 44.5
## Arizona       arizona    8.1     294       80 31.0
## Arkansas     arkansas    8.8     190       50 19.5
## California california    9.0     276       91 40.6
## Colorado     colorado    7.9     204       78 38.7
# create an 'onclick' column
crimes$onclick <- sprintf("window.open(\"%s%s\")",
  "http://en.wikipedia.org/wiki/", as.character(crimes$state) )

gg_crime <- ggplot(crimes, aes(x = Murder, y = Assault, color = UrbanPop )) + 
  geom_point_interactive(
    aes( data_id = state, tooltip = state, onclick = onclick ), size = 3 ) + 
  scale_colour_gradient(low = "#999999", high = "#FF3333")

girafe(ggobj = gg_crime)