This post
explains how to build a basic bump plot with R. It uses
the ggbump package, provides
reproducible code and explain how input data must be
formatted.
For a more advanced usage of ggbump charts,
check out how to customize bump
plot
The ggbump package provides a
geom_bump()
function that allows to build ggbump
charts.
Install the package with install.packages("ggbump")
.
The input dataset is simple: we just have 3 groups, with one value per group and per year. Here is how to build it:
# Library
#install.packages("ggbump")
library(ggbump)
library(tidyverse)
# Create data
year <- rep(2019:2021, 3)
products_sold <- c(
500, 600, 700,
550, 650, 600,
600, 400, 500
)
store <- c(
"Store A", "Store A", "Store A",
"Store B", "Store B", "Store B",
"Store C", "Store C", "Store C"
)
# Create the new dataframe
df <- data.frame(
year = year,
products_sold = products_sold,
store = store
)
It is possible to add individual points to the bump
chart. This is done by adding a geom_point()
layer.
You might be interested in
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥