Learn R Programming

{ggiraph} makes ‘ggplot’ graphics interactive.

Overview

{ggiraph} is a tool that allows you to create dynamic ggplot graphs. This allows you to add tooltips, hover effects and JavaScript actions to the graphics. The package also allows the selection of graphical elements when used in shiny applications.

Interactivity is added to ggplot geometries, legends and theme elements, via the following aesthetics:

  • tooltip: tooltips to be displayed when mouse is over elements.
  • onclick: JavaScript function to be executed when elements are clicked.
  • data_id: id to be associated with elements (used for hover and click actions)

Why use {ggiraph}

  • You want to provide your readers with more information than the basic information available; you can display a tooltip when the user’s mouse is on a graphical element, you can also visually animate elements with the same attribute when the mouse passes over a graphical element, and finally you can link a JavaScript action to the click, such as opening a hypertext link.
  • You want to allow users of a Shiny application to select graphical elements; for example, you can make the points of a scatter plot selectable and available as a reactive value from the server part of your application. With Shiny, {ggiraph} allows interaction with graph elements, legends elements, titles and ggplot theme elements from the server part; each selection is available as a reactive value.

Under the hood, {ggiraph} is an htmlwidget and a ggplot2 extension. It allows graphics to be interactive, by exporting them as SVG documents and using special attributes on the various elements.

Usage

With R and R Markdown

The things you need to know to create an interactive graphic :

  • Instead of using geom_point, use geom_point_interactive, instead of using geom_sf, use geom_sf_interactive… Provide at least one of the aesthetics tooltip, data_id and onclick to create interactive elements.
  • Call function girafe with the ggplot object so that the graphic is translated as a web interactive graphics.
library(ggplot2)
library(ggiraph)
data <- mtcars
data$carname <- row.names(data)

gg_point = ggplot(data = data) +
    geom_point_interactive(aes(x = wt, y = qsec, color = disp,
    tooltip = carname, data_id = carname)) + 
  theme_minimal()

girafe(ggobj = gg_point)

With Shiny

  • If used within a shiny application, elements associated with an id (data_id) can be selected and manipulated on client and server sides. The list of selected values will be stored in in a reactive value named [shiny_id]_selected.

Available interactive layers

They are several available interactive geometries, scales and other ggplot elements. Almost all ggplot2 elements can be made interactive with {ggiraph}. They are all based on their ggplot version, same goes for scales and the few guides: geom_point_interactive(), geom_col_interactive(), geom_tile_interactive(), scale_fill_manual_interactive(), scale_discrete_manual_interactive(), guide_legend_interactive(), …

You can also make interactive annotations, titles and facets (see help(interactive_parameters)).

Installation

Get development version on github

devtools::install_github('davidgohel/ggiraph')

Get CRAN version

install.packages("ggiraph")

Resources

Online documentation

The help pages are located at https://davidgohel.github.io/ggiraph/.

Getting help

If you have questions about how to use the package, visit Stackoverflow and use tags ggiraph and r Stackoverflow link! We usually read them and answer when possible.

Contributing to the package

Bug reports

When you file a bug report, please spend some time making it easy for me to follow and reproduce. The more time you spend on making the bug report coherent, the more time I can dedicate to investigate the bug as opposed to the bug report.

Contributing to the package development

A great way to start is to contribute an example or improve the documentation.

If you want to submit a Pull Request to integrate functions of yours, provide if possible:

  • the new function(s) with code and roxygen tags (with examples)
  • a new section in the appropriate vignette that describes how to use the new function
  • corresponding tests in directory inst/tinytest.

By using rhub (run rhub::check_for_cran()), you will see if everything is ok. When submitted, the PR will be evaluated automatically with Github Action and you will be able to see if something broke.

Copy Link

Version

Install

install.packages('ggiraph')

Monthly Downloads

11,958

Version

0.8.9

License

GPL-3

Maintainer

Last Published

February 24th, 2024

Functions in ggiraph (0.8.9)

geom_count_interactive

Create interactive point counts
geom_bin_2d_interactive

Create interactive heatmaps of 2d bin counts
geom_contour_interactive

Create interactive 2d contours of a 3d surface
geom_map_interactive

Create interactive polygons from a reference map
geom_path_interactive

Create interactive observations connections
geom_rect_interactive

Create interactive rectangles
geom_ribbon_interactive

Create interactive ribbons and area plots
geom_dotplot_interactive

Create interactive dot plots
geom_quantile_interactive

Create interactive quantile regression
geom_raster_interactive

Create interactive raster rectangles
ggiraph

Create a ggiraph object (deprecated)
geom_spoke_interactive

Create interactive line segments parameterised by location, direction and distance
geom_freqpoly_interactive

Create interactive histograms and frequency polygons
ggiraphOutput

Create a ggiraph output element
geom_polygon_interactive

Create interactive polygons
geom_violin_interactive

Create interactive violin plot
geom_label_interactive

Create interactive textual annotations
geom_curve_interactive

Create interactive line segments and curves
geom_jitter_interactive

Create interactive jittered points
geom_point_interactive

Create interactive points
geom_sf_interactive

Create interactive sf objects
GeomInteractive

ggproto classes for ggiraph
geom_smooth_interactive

Create interactive smoothed conditional means
guide_coloursteps_interactive

Create interactive colorsteps guide
guide_legend_interactive

Create interactive legend guide
geom_text_repel_interactive

Create interactive repulsive textual annotations
interactive_curve_grob

Create interactive curve grob
girafe_options

Set girafe options
girafe_css_bicolor

Helper for a 'girafe' css string
girafe_css

CSS creation helper
init_girafe_defaults

Re-init animation defaults options
girafe_defaults

Get girafe defaults formatting properties
interactive_parameters

Interactive parameters
opts_hover

Hover effect settings
labeller_interactive

Construct interactive labelling specification for facet strips
interactive_raster_grob

Create interactive raster grob
interactive_rect_grob

Create interactive rectangle grob
interactive_path_grob

Create interactive path grob
girafeOutput

Create a girafe output element
girafe

Create a girafe object
interactive_points_grob

Create interactive points grob
interactive_circle_grob

Create interactive circles grob
run_girafe_example

Run shiny examples and see corresponding code
scale_alpha_interactive

Create interactive scales for alpha transparency
opts_selection

Selection effect settings
guide_colourbar_interactive

Create interactive continuous colour bar guide
opts_toolbar

Toolbar settings
interactive_roundrect_grob

Create interactive rectangle grob
interactive_polyline_grob

Create interactive polyline grob
guide_bins_interactive

Create interactive bins guide
interactive_polygon_grob

Create interactive polygon grob
opts_sizing

Girafe sizing settings
scale_linetype_interactive

Create interactive scales for line patterns
opts_zoom

Zoom settings
interactive_segments_grob

Create interactive segments grob
opts_tooltip

Tooltip settings
label_interactive

Create an interactive label
interactive_text_grob

Create interactive text grob
renderGirafe

Reactive version of girafe
set_girafe_defaults

Modify defaults girafe animation options
scale_viridis_interactive

Create interactive viridis colour scales
renderggiraph

Reactive version of ggiraph object
scale_gradient_interactive

Create interactive gradient colour scales
scale_colour_interactive

Create interactive colour scales
scale_colour_brewer_interactive

Create interactive colorbrewer scales
scale_colour_steps_interactive

Create interactive binned gradient colour scales
match_family

Find best family match with systemfonts
scale_shape_interactive

Create interactive scales for shapes
scale_manual_interactive

Create your own interactive discrete scale
scale_size_interactive

Create interactive scales for area or radius
validated_fonts

List of validated default fonts
annotation_raster_interactive

Create interactive raster annotations
font_family_exists

Check if font family exists.
geom_bar_interactive

Create interactive bars
geom_abline_interactive

Create interactive reference lines
dsvg

SVG Graphics Driver
facet_wrap_interactive

Create interactive wraped facets
dsvg_view

Run plotting code and view svg in RStudio Viewer or web broswer.
element_interactive

Create interactive theme elements
facet_grid_interactive

Create interactive grid facets
annotate_interactive

Create interactive annotations
geom_crossbar_interactive

Create interactive vertical intervals: lines, crossbars & errorbars
geom_errorbarh_interactive

Create interactive horizontal error bars
geom_density_2d_interactive

Create interactive contours of a 2d density estimate
geom_hex_interactive

Create interactive hexagonal heatmaps
geom_boxplot_interactive

Create interactive boxplot
geom_density_interactive

Create interactive smoothed density estimates