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


It extends ggplot2 with new geom functions:

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

These understand three aesthetics to let you add interactivity:

  • tooltip: column of dataset that contains tooltips to be displayed when mouse is over elements.
  • onclick: column of dataset that contains javascript function to be executed when elements are clicked.
  • 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.

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

## # A tibble: 6 x 11
##   manufacturer model displ  year   cyl trans drv     cty   hwy fl    class
##   <chr>        <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
## 1 audi         a4      1.8  1999     4 auto… f        18    29 p     comp…
## 2 audi         a4      1.8  1999     4 manu… f        21    29 p     comp…
## 3 audi         a4      2    2008     4 manu… f        20    31 p     comp…
## 4 audi         a4      2    2008     4 auto… f        21    30 p     comp…
## 5 audi         a4      2.8  1999     6 auto… f        16    26 p     comp…
## 6 audi         a4      2.8  1999     6 manu… f        18    26 p     comp…
g <- ggplot(mpg, aes( x = displ, y = cty, color = hwy) )


The first example shows how to add a tooltip:

my_gg <- g + geom_point_interactive(aes(tooltip = model), size = 2) 
ggiraph(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) 
ggiraph(code = print(my_gg), hover_css = "cursor:pointer;fill:red;stroke:red;")

The default value of hover css is hover_css = "fill:orange;". Read more about custom animation effects

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

Click actions

Using onclick

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

##                 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("\"%s%s\")",
  "", as.character(crimes$state) )

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

ggiraph(code = print(gg_crime), hover_css = "fill-opacity:.3;cursor:pointer;")

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. More details are available in vignette “ggiraph in shiny”. 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

Custom animation effects

With ggiraph, you can customize tooltip style and mouse hover effects. This requires usage of css.

Tooltip position

The arguments tooltip_offx and tooltip_offy are used to offset tooltip position.

By default the offset is 10 pixels horizontally to the mouse position (tooltip_offx=10) and 0 pixels vertically (tooltip_offy=0).


dataset <- mtcars
dataset$carname <- row.names(dataset)
gg_point_1 <- ggplot(dataset, aes(x = disp, y = qsec, tooltip = carname, data_id = carname, color= wt) ) + 

# htmlwidget call
ggiraph(code = {print(gg_point_1)}, tooltip_offx = 20, tooltip_offy = -10 )

Tooltip style

The ggiraph function has an argument named tooltip_extra_css. It can be used to add css declarations to customize tooltip rendering.

Each css declaration includes a property name and an associated value. Property names and values are separated by colons and name-value pairs always end with a semicolon. For example color:gray;text-align:center;. Common properties are :

  • background-color: background color
  • color: elements color
  • border-style, border-width, border-color: border properties
  • width, height: size of tooltip
  • padding: space around content

Tooltip opacity can be defined with the argument tooltip_opacity (default to 0.9).

Let’s custom tooltip as:

  • italic font
  • no background color

Now print the ggiraph:

ggiraph(code = {print(gg_point_1)}, tooltip_extra_css = tooltip_css )

Now, let’s add a gray rectangle with round borders and a few other details to make it less crude:

tooltip_css <- "background-color:gray;color:white;font-style:italic;padding:10px;border-radius:10px 20px 10px 20px;"

ggiraph(code = {print(gg_point_1)}, tooltip_extra_css = tooltip_css, tooltip_opacity = .75 )

Do not surround tooltip_extra_css value by curly braces, ggiraph takes care of that.

Hover effects

Hover effects occur when the mouse is over elements that have a data-id attribute (resulting from using argument data_id in interactive geom functions). It will only modify SVG element rendering when the mouse is over an element.

Mouse over effects can be configured with the hover_css argument in the same way tooltip_extra_css is used for customizing tooltip rendering.

css here is relative to SVG elements. SVG attributes are listed here. Common properties are:

  • fill: background color
  • stroke: color
  • stroke-width: border width
  • r: circle radius (no effect if Firefox is used).

To fill elements in red:

ggiraph(code = {print(gg_point_1)}, hover_css = "fill:red;r:10pt;" )


You can activate zoom; set zoom_max (maximum zoom factor) to a value greater than 1. If the argument is greater than 1, a toolbar will appear when mouse will be over the graphic.

Click on the icons in the toolbar to activate or desactivate the zoom.

ggiraph(code = print(gg_point_1), zoom_max = 5)

Shiny usage

ggiraph is an htmlwidget. It can be used within a Shiny application. In shiny, elements associated with data_id can be selected and the selection (the data_id value) is available in the client and the server side of the application.

The client ui.R

Instead of a plotOutput, use function ggiraphOutput.

The server server.R

Use function ggiraphOutput.

output$plot <- renderggiraph({
    ggiraph(code = print(gg_blahblah) )


It is possible to work with selected points on a ggiraph plot within a Shiny application.

Selection can be of two types: single or multiple. The ggiraph’s parameter selection_type will let you specify that.

output$myplot <- renderggiraph({
    ggiraph(code = print(gg_blahblah), selection_type = "multiple" )

The selected points will be captured in the input reactive value myplot_selected (name of the input id of the reactive output value + _selected):

You can also modify theses values by using the session$sendCustomMessage method with type myplot_set (name of the input id of the reactive output value + _set).


The package contains Shiny examples available in the shiny directory of the package (system.file("shiny", package = "ggiraph")).

selections usage (server side)

shiny::runApp(appDir = system.file("shiny/crimes", package = "ggiraph"), display.mode = "showcase")
shiny::runApp(appDir = system.file("shiny/cars", package = "ggiraph"), display.mode = "showcase")

# *group* selection
shiny::runApp(appDir = system.file("shiny/iris", package = "ggiraph"), display.mode = "showcase")

onclick actions (client side).

shiny::runApp(appDir = system.file("shiny/DT", package = "ggiraph"), display.mode = "showcase")