Bump charts
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
::install_github("davidsjoberg/ggbump")
devtoolslibrary(ggbump)
install.packages("fivethirtyeight")
library(fivethirtyeight)
library(ggplot2)
DATA:
We’ll use the fivethirtyeight::tv_hurricanes
data, but slightly restructured and filtered.
Code
::tv_hurricanes |>
fivethirtyeightfilter(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(title = "TV News Hurricane Mentions",
labs_bump subtitle = "Between Sep 15-25th, 2017",
x = "Date", y = "Rank",
color = "Hurricanes")
<- ggplot(tidy_hurricanes,
ggp2_bump aes(x = date,
y = rank,
color = hurricane)) +
::geom_bump(size = 2) +
ggbumptheme(legend.position = "bottom")
+
ggp2_bump labs_bump
GRAPH: