The R Script associated with this page is available here. Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.


In this module we will explore several ways to generate dynamic and interactive data displays. These include making maps and graphs that you can pan/zoom, select features for more information, and interact with in other ways. The most common output format is HTML, which can easily be embedded in a website (such as your final project!).


If you don’t have the packages above, install them in the package manager or by running install.packages("doParallel").


DataTables display R data frames as interactive HTML tables (with filtering, pagination, sorting, and search). This is a great way to make your raw data browsable without using too much space.

datatable(iris, options = list(pageLength = 5))


Interface to the Bokeh library for making interactive graphics.

figure(width = 400, height=400) %>%
  ly_points(Sepal.Length, Sepal.Width, data = iris,
    color = Species, glyph = Species,
    hover = list(Sepal.Length, Sepal.Width))


Leaflet is a really powerful JavaScript library for creating dynamic maps that support panning and zooming along with various annotations like markers, polygons, and popups. The example below were adapted from the leaflet vignettes.

geocode("Buffalo, NY")
##         lon      lat
## 1 -78.87837 42.88645
m <- leaflet() %>% setView(lng = -78.87837, lat = 42.88645, zoom = 12) %>% 
frameWidget(m,height =500)

Your turn

This example only scratches the surface of what is possible with leaflet. Consider whether you can use an leaflet maps in your project.

  • Browse the Leaflet website
  • What data could you use?
  • How would you display it?


An R interface to the ‘dygraphs’ JavaScript charting library. Provides rich facilities for charting time-series data in R, including highly configurable series- and axis-display and interactive features like zoom/pan and series/point highlighting.

dygraph(nhtemp, main = "New Haven Temperatures",height = 100) %>% 
  dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))%>%
  frameWidget(height =500)

Your turn

Make a dygraph of recent daily maximum temperature data from Buffalo, NY.


  • Use the following code to download the daily weather data (if this is taking too long, you can use the nhtemps object loaded above)
## Loading required package: zoo
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##     as.Date, as.Date.numeric
## Attaching package: 'xts'
## The following object is masked from 'package:leaflet':
##     addLegend
## The following objects are masked from 'package:dplyr':
##     first, last
                   date_min = "2016-01-01", 
                   var = c("TMAX"),
  • create a xts time series object as required by dygraph() using xts() and specify the vector of data and the date column (see ?xts for help).
  • use dygraph() to draw the plot
  • add a dyRangeSelector() with a dateWindow of c("2017-01-01", "2017-12-31")
# Convert to a xts time series object as required by dygraph

dygraph(dt, main = "Daily Maximum Temperature in Buffalo, NY") %>% 
  dyRangeSelector(dateWindow = c("2017-01-01", "2017-12-31"))%>%
  frameWidget(height =500)


Create interactive 3D scatter plots, network plots, and globes using the ‘three.js’ visualization library.

z <- seq(-10, 10, 0.1)
x <- cos(z)
y <- sin(z)
scatterplot3js(x, y, z, color=rainbow(length(z)))


Creates ‘D3’ ‘JavaScript’ network, tree, dendrogram, and Sankey graphs from ‘R’.


Load example network

This loads an example social network of friendships between 34 members of a karate club at a US university in the 1970s. See W. W. Zachary, An information flow model for conflict and fission in small groups, Journal of Anthropological Research 33, 452-473 (1977).

karate <- make_graph("Zachary")
wc <- cluster_walktrap(karate)
members <- membership(wc)

# Convert to object suitable for networkD3
karate_d3 <- igraph_to_networkD3(karate, group = members)

Force directed network plot

forceNetwork(Links = karate_d3$links, Nodes = karate_d3$nodes,
             Source = 'source', Target = 'target', NodeID = 'name',
             Group = 'group')%>%
  frameWidget(height =500)

Sankey Network graph

Sankey diagrams are flow diagrams in which the width of the arrows is shown proportionally to the flow quantity.

# Load energy projection data
URL <- paste0(
Energy <- fromJSON(URL)
sankeyNetwork(Links = Energy$links, Nodes = Energy$nodes, Source = "source",
             Target = "target", Value = "value", NodeID = "name",
             units = "TWh", fontSize = 12, nodeWidth = 30)%>%
  frameWidget(height =500)

Radial Network

URL <- paste0(

## Convert to list format
Flare <- jsonlite::fromJSON(URL, simplifyDataFrame = FALSE)
# Use subset of data for more readable diagram
Flare$children = Flare$children[1:3]

radialNetwork(List = Flare, fontSize = 10, opacity = 0.9, height = 400, width=400)

Diagonal Network

diagonalNetwork(List = Flare, fontSize = 10, opacity = 0.9, height = 400, width=400)


RGL provides 3D interactive graphics, including functions modelled on base graphics (plot3d(), etc.) as well as functions for constructing representations of geometric objects (cube3d(), etc.). You may need to install XQuartz.


# Load a low-resolution elevation dataset of a volcano

Plot an interactive 3D surface

persp3d(volcano, type="s",col="green3")
rglwidget(elementId = "example", width = 500, height = 400)%>%

Your turn

Check out the HTML Widgets page for many more examples.

Which can you use in your project?