Change over time

Materials for class on Tuesday, October 10, 2017



Download the slides from today’s lecture.

First slide

Make this plot


See complete column descriptions. The data is released under a public domain license and hosted originally at Kaggle.

Louisville animal bites



Final figure


bites_raw <- read_csv("data/Health_AnimalBites.csv")

bites <- bites_raw %>%
  mutate(year = year(bite_date)) %>%
  mutate(species = recode(SpeciesIDDesc, 
                          CAT = "Cat", DOG = "Dog", .default = "Other")) %>%
  mutate(species = factor(species, levels = c("Dog", "Cat", "Other"), ordered = TRUE)) %>%
  filter(year < 2018, year > 2010) 

bites_plot_year <- bites %>%
  filter(! %>%
  group_by(year, species) %>%
  summarize(total_bites = n())

fancy_theme <- function(base_size = 11) {
  theme_minimal(base_family = "Roboto Condensed", base_size = base_size) +
  theme(plot.title = element_text(family = "Roboto Condensed", face = "bold",
                                  size = rel(1.5)),
        plot.caption = element_text(family = "Roboto Condensed Light", face = "plain",
                                    size = rel(0.7)),
        plot.subtitle = element_text(family = "Roboto Condensed Light", face = "plain",
                                     size = rel(1.1)),
        legend.position = "bottom",
        panel.grid.minor = element_blank())

nice_plot <- ggplot(bites_plot_year, aes(x = year, y = total_bites, color = species, group = species)) +
  geom_line(size = 2) + 
  labs(x = NULL, y = "Total number of bites", 
       title = "Is the Louisville canine rampage over?", 
       subtitle = "Total number of reported animal bites per year",
       caption = "Source: Louisville Metro Department of Public Health and Wellness") + 
  guides(color = guide_legend(title = NULL)) +
  scale_color_manual(values = c("#F05A2A", "#2FCBC5", "#37291E")) +
  scale_x_continuous(breaks = seq(2011, 2017, 1)) +
  fancy_theme(base_size = 18)


Refining and enhancing plots in Illustrator

Install a vector image editor

Make an image and refine it

Data: Nathan’s Famous Hot Dog contest winners Data originally from FlowingData.

Plot with transparent background:

Text for annotations:

Enhanced plot:

Enhanced hot dog eating contest graph

Enhanced hot dog eating contest graph

Bonus ggplot features


So far, we’ve used a few extra packages to enhance our ggplot plots, like ggrepel, gridExtra, and ggThemeAssist. There are dozens of other ggplot-related packages, but it’s hard to find them or know what exists. The ggplot2 Extensions Gallery helpfully lists 35As of October 9, 2017

different packages.


You can use the gganimate package to add a new aesthetic “frame” to your plots, meaning you can map a variable like “year” to a plot to show changes over time.

Install the package in R with this command: (it’s not on CRAN so you can’t use the “Packages” panel):

Before you use it, though, you have to install two command line tools on your computer to manipulate images and videos. You don’t actually have to ever use these programs—they just have to be installed, and gganimate will use them.

Installation on Windows

  1. Download ImageMagick for Windows and run the installer.
  2. Make sure you select these options: ImageMagick installation options for Windows

    • Add application directory to your system path
    • Install FFmpeg (this will save you from having to install FFMpeg separately)
    • Install legacy utilities
  3. Restart your computer (or at least log out and log back in)

Installation on macOS

Because both ImageMagick and FFmpeg are Linux-based programs, and because underneath its fancy bells and whistles, macOS is essentially a Unix system, there’s no automatic installer for macOS. You have to install ImageMagick and FFmpeg with Terminal instead, but it’s not hard!

  1. Open Terminal
  2. Go to, copy the big long command under “Install Homebrew” (starts with /usr/bin/ruby -e "$(curl -fsSL...), paste it into Terminal, and press enter. This installs Homebrew, which is special software that lets you install Unix-y programs from the terminal. You can install stuff like MySQL, Python, Apache, and even R if you want; there’s a full list of Homebrew formulae here.

  3. Type these three lines in Terminal:

     brew install imagemagick
     brew install libvpx
     brew install ffmpeg --with-libvpx
  4. Wait for a few minutes while your Terminal makes you feel like a hacker.

  5. The end. You shouldn’t have to restart or log out.


We won’t cover this in very much depth in class—look at the documentation at GitHub for full details. Here’s a quick example, though. Note the new frame = year aesthetic mapping:

Animated Gapminder plot

Animated Gapminder plot

You can also save the animation to your computer:

You can adjust the animation speed with the interval argument in gganimate():

Fast animated Gapminder plot

Fast animated Gapminder plot

To use this in an R Markdown document, you have to include"animate" in the chunk options, otherwise each individual frame will be shown. If you’re using a custom interval, you have to specify it with interval=X. You can also specify the output format for the HTML chunk (e.g. gif, webm, mp4, etc.):

{r name-of-chunk,"animate", interval=0.2, ffmpeg.format="gif", warning=FALSE, message=FALSE}

These animations tend to not be very smooth, though, with data points jumping to their new positions every five years (in the case of the Gapminder data). We can make everything go a lot smoother with the tweenr package, which interpolates data points to smooth everything out. You have to transform the data little first, but the end result is magical:This code is from David Smith.

Feedback for today

Go to this form and answer these three questions (anonymously if you want):

  1. What new thing did you learn today?
  2. What was the most unclear thing about today?
  3. What was the most exciting thing you learned today?