library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
transactions <- get_transactions()
#products <- products
#demographics <- demographics
vitamin <- transactions %>%
inner_join(products, by = 'product_id') %>%
filter(product_category == "VITAMINS") %>%
inner_join(demographics, by = "household_id") %>%
group_by(income) %>%
summarize(total_vitamin_purchased = sum(quantity, na.rm = TRUE), .groups = "drop") %>%
arrange(income)
# Create a bar chart of Vitamins Purchased to Income level
ggplot(data = vitamin, aes(x = factor(income, levels = unique(income)), y = total_vitamin_purchased, fill = income)) + geom_col() +
labs(
title = "Vitamin Purchases by Income Level",
subtitle = "Total quantity of vitamins purchased across different income levels",
x = "Income Level",
y = "Total Quantity of Vitamins Purchased"
) +
theme_minimal()

brand_sales <- transactions %>%
inner_join(products, by = "product_id") %>%
group_by(brand) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE), .groups = "drop") %>%
arrange(desc(total_sales))
brand_sales <- brand_sales %>%
mutate(percentage = total_sales / sum(total_sales) * 100)
#National Brands vs. Private Labels
ggplot(brand_sales, aes(x = "", y = percentage, fill = brand)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y", start = 0) +
labs(
title = "National Brands vs. Private Labels",
subtitle = "Percentage share of total sales",
y = NULL,
x = NULL
) +
scale_y_continuous(labels = scales::percent_format(scale = 1)) + # Convert to percentage labels
theme_minimal()

age_product_sales <- transactions %>%
inner_join(products, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
group_by(age, product_category) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE), .groups = "drop") %>%
arrange(age, desc(total_sales)) %>%
group_by(age) %>%
slice_head(n = 5)
#Most Purchased Product Categories per Age Group
ggplot(age_product_sales, aes(x = total_sales, y = product_category, fill = age)) +
geom_col() +
facet_wrap(~age, scales = "free_y", ncol = 2) +
labs(
title = "Top 5 Most Purchased Product Categories per Age Group",
subtitle = "Comparing product preferences across different age demographics",
x = "Total Sales ($)",
y = "Product Category"
) +
theme_minimal()
