library(tidyverse)
library(completejourney)
library(dplyr)
library(ggplot2)
Plot 1: Who’s Buying the Most Desserts and Pizzas?
products %>%
filter(str_detect(product_category, regex("(FROZEN PIE/DESSERTS)|(FROZEN PIZZA)", ignore_case = TRUE))) %>%
inner_join(transactions_sample, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
group_by(income, age) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
mutate(age = fct_na_value_to_level(age, level = "Unknown")) %>%
ggplot(aes(x = age, y = total_sales)) +
geom_col(fill = "skyblue") +
facet_wrap(~ income) +
scale_x_discrete("Age Group") +
scale_y_continuous("Total Frozen Dessert and Pizza Sales", labels = scales::dollar) +
ggtitle("Who's Buying Frozen Food by Age Group and Income?",
subtitle = "The age group from 45-54 with an income of $50-74K buy the most desserts and pizza
products.")

Plot 2: Spending on Smartphones and Accessories by Households who
have Kids
products %>%
filter(str_detect(product_category, regex("(Smartphones)|(Accessories)",
ignore_case = TRUE))) %>%
inner_join(transactions_sample, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
filter(kids_count == 1) %>%
group_by(household_id) %>%
summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
top_n(5, total_sales) %>%
ggplot(aes(x = reorder(household_id, -total_sales), y = total_sales, shape = as.factor(household_id))) +
geom_point(size = 3) +
scale_x_discrete("Household ID") +
scale_y_continuous("Total Smartphone and Accessories Sales", labels = scales::dollar) +
ggtitle("Top 5 Households with Kids Buying Smartphones and Accessories",
subtitle = "Households 1304 has highest total sales of smartphones and accessories.") +
guides(shape = guide_legend(title = NULL))

Plot 3: What households have used the most amount of coupons across
products?
coupon_redemptions %>%
inner_join(coupons, by = "coupon_upc") %>%
inner_join(products, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
group_by(household_id, coupon_upc) %>%
summarise(total_coupons = n()) %>%
group_by(household_id) %>%
summarise(total_coupons_across_products = sum(total_coupons)) %>%
arrange(desc(total_coupons_across_products)) %>%
top_n(10, total_coupons_across_products) %>%
ggplot(aes(x = reorder(as.factor(household_id), -total_coupons_across_products), y = total_coupons_across_products, group = 1)) +
geom_line(color = "skyblue", size = 1) +
geom_point(color = "blue", size = 3) +
scale_x_discrete("Household ID") +
scale_y_continuous("Total Coupons Used Across Products") +
ggtitle("Top 10 Households with the Most Coupon Usage Across Products",
subtitle = "Bar chart showing total coupons used for each household.")
