#Source: https://t.me/rstudioprogr
library(ggplot2)
library(ggbump)
library(tidyverse)
# Create data
year <- rep(2019:2023, 3)
products_sold <- c(
500, 600, 700, 550, 800,
550, 680, 600, 666, 700,
600, 400, 500, 450, 520
)
store <- c(
"Store A", "Store A", "Store A","Store A", "Store A",
"Store B", "Store B", "Store B","Store B", "Store B",
"Store C", "Store C", "Store C","Store C", "Store C"
)
# Create the new dataframe
df <- data.frame(
year = year,
products_sold = products_sold,
store = store
)
# Calculate percentage change for each store
df <- df %>%
group_by(store) %>%
mutate(perc_change = (products_sold / lag(products_sold) - 1) * 100) %>%
ungroup()
# Create the plot
ggplot(df, aes(x = year, y = products_sold, color = store, group = store)) +
geom_bump(size = 2) +
geom_point(size = 6) +
geom_text(aes(label = store), nudge_y = 30, fontface = "bold", size=4) +
scale_color_viridis_d(option = "D") + # Changed color palette to Viridis
theme_minimal(base_size = 15) +
labs(
title = "Products Sold per Store (2019-2023)",
x = "Year",
y = "Products Sold",
caption = "Note: Percentage change calculated year-to-year."
) +
geom_text(data = df %>% filter(!is.na(perc_change)),
aes(y = products_sold - 20, label = paste0(round(perc_change, 1), "%")),
size = 3.5, color = "black", fontface = "italic")
