Introduction

Our next DataCamp lesson introduces animation using a package called plotly, which “wraps” around ggplot2 objects and allows for interactivity and animation with techniques like brushing, zooming, panning, animation and more.

This document

This page was built as an RNotebook using rmarkdown and then “knitted” with knitr . Most of the examples and code are taken and adapted from The Plotly Cookbook by Carson Sievert, available online at https://plotly-book.cpsievert.me/index.html

The code and examples

We start by installing and calling several r packages

library(tidyverse)
library(plotly)
library(dplyr)
library(gapminder)

The first few examples use some data about Texas housing prices, which is bundled with ggplot2. The relevant data frame is called txhousing.

  1. Several ways to manage overplotting, with brushing added:
p <- ggplot(txhousing, aes(date, median)) +
     geom_line(aes(group = city), alpha = 0.2)
subplot(
     p, ggplotly(p, tooltip = "city"), 
     ggplot(txhousing, aes(date, median)) + geom_bin2d(),
     ggplot(txhousing, aes(date, median)) + geom_hex(),
     nrows = 2, shareX = TRUE, shareY = TRUE,
     titleY = FALSE, titleX = FALSE
)
  1. There are many cities in Texas. Here is a multi-line graph, highlighting one city.
tx <- group_by(txhousing, city)
# initiate a plotly object with date on x and median on y
p <- plot_ly(tx, x = ~date, y = ~median)


# add a line highlighting houston
add_lines(
     # plots one line per city since p knows city is a grouping variable
     add_lines(p, alpha = 0.2, name = "Texan Cities", hoverinfo = "none"),
     name = "Houston", data = filter(txhousing, city == "Houston")
)

More examples using the mtcars data frame

You may recall the mtcars data frame in r. It contains data about car characteristics, compiled by Motor Trend magazine.

glimpse(mtcars)
## Observations: 32
## Variables: 11
## $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19....
## $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 1...
## $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, ...
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
## $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
## $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
## $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...
head(mtcars)
  1. Allow panning (moving the entire plot within a window)
p <- ggplot(fortify(mtcars), aes(wt, mpg)) + 
     geom_point() + geom_smooth()
gg <- ggplotly(p)
layout(gg, dragmode = "pan")
  1. Add a range slider, as in Google Finance
p <- ggplot(fortify(mtcars), aes(wt, mpg)) + 
     geom_point() + geom_smooth()
gg <- ggplotly(p)
rangeslider(gg)
  1. How about a 3-D scatterplot?
plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
  add_markers(color = ~cyl)
  1. More animation, a la Tableau and Hans Rosling
data(gapminder, package = "gapminder")

meanLife <- with(gapminder, tapply(lifeExp, INDEX = continent, mean))
gapminder$continent <- factor(
     gapminder$continent, levels = names(sort(meanLife))
)
base <- gapminder %>%
     plot_ly(x = ~gdpPercap, y = ~lifeExp, size = ~pop, 
             text = ~country, hoverinfo = "text") %>%
     layout(xaxis = list(type = "log"))

base %>%
     add_markers(color = ~continent, alpha = 0.2, showlegend = F) %>%
     add_markers(color = ~continent, frame = ~year, ids = ~country) %>%
     animation_opts(1500, redraw = FALSE)

Conclusion

Animation and interactivity can help in exploration, presentation, and interactive story-telling. Several R pacakges are available to facilitate the process, and in some cases to offer capabilities not yet available in Tableau.