YoY Chart by Category

Author

Francisco Zambrano

Published

July 1, 2023

Installation

Development version from GitHub:

GitHub: https://github.com/zpio/ggexplorer

Show/Hide code
remotes::install_github("zpio/ggexplorer")
Show/Hide code
library(ggexplorer)

Examples

Superstore Dataset

Show/Hide code
data <- readr::read_csv(
  file = "https://raw.githubusercontent.com/zpio/datasets/main/sample_superstore.csv"
)

data

Basic Chart

Show/Hide code
yoy_barchart(
  data = data,
  date = order_date,
  category = state,
  value = sales,
  current_year = 2021,
  previous_year = 2020,
  title = "Sales by Category 2021 (YoY %)"
)

Top N

Show/Hide code
yoy_barchart(
  data = data,
  date = order_date,
  category = state,
  value = sales,
  current_year = 2021,
  previous_year = 2020,
  title = "Sales by Category 2021 (YoY %)",
  top_n = 5
)

Colors

Show/Hide code
yoy_barchart(
  data = data,
  date = order_date,
  category = state,
  value = sales,
  current_year = 2021,
  previous_year = 2020,
  title = "Sales by Category 2021 (YoY %)",
  palette_gradient = "Green-Gold"
)

https://jrnold.github.io/ggthemes/reference/scale_colour_gradient_tableau.html

SparkLine color

Show/Hide code
yoy_barchart(
  data = data,
  date = order_date,
  category = state,
  value = sales,
  current_year = 2021,
  previous_year = 2020,
  title = "Sales by Category 2021 (YoY %)",
  ts_linecolor = "green3"
)

Interactive

Show/Hide code
yoy_barchart(
  data = data,
  date = order_date,
  category = state,
  value = sales,
  current_year = 2021,
  previous_year = 2020,
  title = "Sales by Category 2021 (YoY %)",
  interactive = TRUE
)

Show/Hide Label

Show/Hide code
yoy_barchart(
  data = data,
  date = order_date,
  category = state,
  value = sales,
  current_year = 2021,
  previous_year = 2020,
  title = "Sales by Category 2021 (YoY %)",
  interactive = TRUE,
  show_label = FALSE
)

Stat-Identity

Show/Hide code
data_sales <- data.frame(
  category = c("Furniture","Office Supplies","Technology"),
  sales = c(414288.7, 430037.2, 498095.0),
  sales_2021 = c(215387.3, 246097.2, 271730.8),
  sales_2020 = c(198901.4, 183940.0, 226364.2)
)
data_sales
Show/Hide code
yoy_barchart0(
  data = data_sales,
  category = category,
  cy_var = sales_2021,
  py_var = sales_2020
)