# Load necessary packages
library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
# Plot 1
ice_cream_sales_income <- transactions_sample %>%
inner_join(products, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
filter(grepl("ice cream", product_category, ignore.case = TRUE)) %>%
group_by(income) %>%
summarise(total_sales_value = sum(sales_value, na.rm = TRUE)) %>%
arrange(desc(total_sales_value))
ice_cream_sales_income$highlight <- ifelse(ice_cream_sales_income$total_sales_value == max(ice_cream_sales_income$total_sales_value), "highlight", "normal")
ggplot(ice_cream_sales_income, aes(x = reorder(income, -total_sales_value), y = total_sales_value, fill = highlight)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("highlight" = "lightgreen", "normal" = "skyblue")) +
geom_text(aes(label = scales::dollar(total_sales_value)),
vjust = -0.5, size = 4,
color = "black",
data = ice_cream_sales_income %>% filter(highlight == "highlight")) +
labs(title = "Average Spent on Ice Cream by Income Level (2017)",
x = "Income Level",
y = "Average Spent") +
scale_y_continuous(labels = scales::dollar_format()) +
theme_minimal() +
theme(legend.position = "none")

# Plot 2
coupon_redemptions_with_coupons <- coupon_redemptions %>%
inner_join(coupons, by = "coupon_upc", relationship = "many-to-many")
coupon_redemptions_with_products <- coupon_redemptions_with_coupons %>%
inner_join(products, by = "product_id") %>%
filter(grepl("ice cream", product_category, ignore.case = TRUE))
ice_cream_coupons_agg <- coupon_redemptions_with_products %>%
group_by(household_id) %>%
summarise(coupon_count = n())
ice_cream_coupons <- ice_cream_coupons_agg %>%
inner_join(transactions_sample, by = "household_id") %>%
mutate(month = floor_date(transaction_timestamp, "month")) %>%
group_by(month) %>%
summarise(coupon_redemptions = sum(coupon_count))
peak_month <- ice_cream_coupons %>%
filter(coupon_redemptions == max(coupon_redemptions))
ggplot(ice_cream_coupons, aes(x = month, y = coupon_redemptions)) +
geom_line(color = "lightgreen", linewidth = 1.2) +
geom_point(data = peak_month, aes(x = month, y = coupon_redemptions), color = "skyblue", size = 5) +
geom_text(data = peak_month, aes(x = month, y = coupon_redemptions, label = coupon_redemptions),
vjust = -1, size = 3, color = "black") +
labs(title = "Total Ice Cream Coupon Usage by Month (2017)",
x = "Month",
y = "Coupon Uses") +
theme_minimal()

# Plot 3
# Aggregate by household size to calculate average spending per transaction
avg_ice_cream_spending_household <- transactions_sample %>%
inner_join(products, by = "product_id") %>%
inner_join(demographics, by = "household_id") %>%
filter(grepl("ice cream", product_category, ignore.case = TRUE)) %>%
group_by(household_size) %>%
summarise(avg_spending = mean(sales_value, na.rm = TRUE)) %>%
arrange(desc(avg_spending))
avg_ice_cream_spending_household$highlight <- ifelse(avg_ice_cream_spending_household$household_size == "5+", "highlight", "normal")
ggplot(avg_ice_cream_spending_household, aes(x = reorder(household_size, -avg_spending), y = avg_spending, fill = highlight)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("highlight" = "skyblue", "normal" = "lightgreen")) +
geom_text(aes(label = scales::dollar(avg_spending)),
vjust = -0.5, size = 4,
color = "black",
data = avg_ice_cream_spending_household %>% filter(highlight == "highlight")) +
labs(title = "Ice Cream Spending by Household Size (2017)",
x = "Household Size",
y = "Average Spent per Transaction") +
scale_y_continuous(labels = scales::dollar_format()) +
theme_minimal() +
theme(legend.position = "none")
