Graph info

Should I use this graph?


This graph requires:

✅ a categorical variable

✅ a numeric (continuous) variable

✅ a numeric (date) variable

Description

Bump charts show how numerical (ranked) values change over time for different categories (or groups). Differences are represented with connecting lines (along the y axis) that cover the full timescale (along the x axis).

We can build bump charts in ggplot2 with the ggbump package:

Getting set up

PACKAGES:

Install packages.

Code
devtools::install_github("davidsjoberg/ggbump")
library(ggbump)
install.packages("fivethirtyeight")
library(fivethirtyeight) 
library(ggplot2)

DATA:

We’ll use the fivethirtyeight::tv_hurricanes data, but slightly restructured and filtered.

Code
fivethirtyeight::tv_hurricanes |> 
  filter(date > as_date("2017-09-15")) |> 
  pivot_longer(cols = -date, 
    names_to = 'hurricane', 
    values_to = 'value') |> 
  group_by(date) |>
  mutate(rank = rank(value, 
    ties.method = "random")) |> 
  ungroup() -> tidy_hurricanes
glimpse(tidy_hurricanes)
Rows: 40
Columns: 4
$ date      <date> 2017-09-16, 2017-09-16, 2017-09-16, 2017-09-16, 2017-09-17,…
$ hurricane <chr> "harvey", "irma", "maria", "jose", "harvey", "irma", "maria"…
$ value     <dbl> 0.0207, 0.1087, 0.0000, 0.0355, 0.0087, 0.1090, 0.0184, 0.03…
$ rank      <int> 2, 4, 1, 3, 1, 4, 2, 3, 1, 3, 4, 2, 1, 3, 4, 2, 2, 3, 4, 1, …

The grammar

CODE:

Create labels with labs()

Map date to the x, rank to the y, and hurricane to color

Add ggbump::geom_bump() and set size to 2

Move legend to bottom with theme(legend.position = "bottom")

Code
labs_bump <- labs(title = "TV News Hurricane Mentions",   
  subtitle = "Between Sep 15-25th, 2017", 
  x = "Date", y = "Rank", 
  color = "Hurricanes")
ggp2_bump <- ggplot(tidy_hurricanes, 
    aes(x = date, 
      y = rank, 
      color = hurricane)) +
    ggbump::geom_bump(size = 2) + 
    theme(legend.position = "bottom")

ggp2_bump + 
  labs_bump

GRAPH: