How to plot Bump Chart in R?

[This article was first published on Data Analysis in R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to plot Bump Chart in R? appeared first on finnstats.

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))

If you are interested to learn more about data science, you can find more articles here finnstats.

The post How to plot Bump Chart in R? appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)