How to plot Bump Chart in R?
How to plot Bump Chart in R?, A bump chart is a sort of chart that emphasizes the order of the groups rather than the quantity of change by displaying rankings of various groups through time as opposed to absolute numbers.
This article shows you how to use ggplot2 to quickly generate a bump chart in R.
How to plot Bump Chart in R
dplyr and ggplot2 must be loaded in order to build a bump chart in R.
library(ggplot2) library(dplyr)
Then, we’ll generate some data to use:
To make this example reproducible, set the seed to
set.seed(123) df<- data.frame(team = rep(LETTERS[1:5], each = 10), random_num = runif(50), day = rep(1:10, 5))
head(df)
team random_num day 1 A 0.2875775 1 2 A 0.7883051 2 3 A 0.4089769 3 4 A 0.8830174 4 5 A 0.9404673 5 6 A 0.0455565 6
df<- df %>% group_by(day) %>% arrange(day, desc(random_num), team) %>% mutate(rank = row_number()) %>% ungroup() head(df)
team random_num day rank <chr> <dbl> <int> <int> 1 D 0.963 1 1 2 B 0.957 1 2 3 C 0.890 1 3 4 A 0.288 1 4 5 E 0.143 1 5 6 D 0.902 2 1
This data frame merely displays the “rank” of five distinct teams during a 10-day period.
The ranking of each team for each day during this period may be seen using a bump chart made with ggplot2.
ggplot(df, aes(x = day, y = rank, group = team)) + geom_line(aes(color = team, alpha = 1), size = 2) + geom_point(aes(color = team, alpha = 1), size = 4) + scale_y_reverse(breaks = 1:nrow(df))