library(tidyverse)
library(completejourney)
library(ggalluvial)
transactions<- get_transactions()
data("coupons")
spending_by_household_size_income <- transactions %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(household_size, income) %>%
  summarize(total_spending = sum(sales_value, na.rm = TRUE),
            avg_spending = mean(sales_value), 
            .groups = "drop")

ggplot(spending_by_household_size_income, aes(x = as.factor(household_size), y = total_spending, fill = income)) +
  geom_bar(stat = "identity", position = "stack") +
  labs(title = "Total Spending by Household Size, Segmented by Income Level",
       x = "Household Size",
       y = "Total Spending ($)",
       fill = "Income Level") +
  theme_minimal(base_size = 12)

products %>%
  filter(department == "GROCERY") %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(income) %>%
  mutate(households = n_distinct(household_id)) %>%
  mutate(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = total_sales, y = income, size = households, color = income)) +
  geom_point(shape = 21,fill = "lightblue", stroke = 1, alpha = 0.7) +        
  labs(title = "Total Sales vs Income Groups for GROCERY Department",
       subtitle = "Analysis of Grocery Shopping by Different Income Groups",
       x = "Total Value of Grocery Shopping",
       y = "Income Groups",
       color = "Income Level",
       size = "Number of Households") +
  scale_x_continuous(labels = scales::dollar) +
  theme_minimal()  

coupon_baskets <- transactions %>%
  filter(coupon_disc > 0) %>% 
  select(household_id, basket_id) %>%
  distinct()

total_baskets <- transactions %>%
  select(household_id, basket_id) %>%
  distinct()

coupon_data <- coupon_baskets %>%
  left_join(demographics, by = "household_id") %>%
  filter(!is.na(income), !is.na(household_size))

total_data <- total_baskets %>%
  left_join(demographics, by = "household_id") %>%
  filter(!is.na(income), !is.na(household_size))

redemption_rates <- coupon_data %>%
  group_by(income, household_size) %>%
  summarize(coupon_basket_count = n()) %>%
  left_join(
    total_data %>%
      group_by(income, household_size) %>%
      summarize(total_basket_count = n()),
    by = c("income", "household_size")
  ) %>%
  mutate(
    redemption_rate = coupon_basket_count / total_basket_count
  ) %>%
  filter(total_basket_count >= 50)  

income_levels <- c(
  "Under 15K", "15-24K", "25-34K", "35-49K",
  "50-74K", "75-99K", "100-124K", "125-149K", "150K+"
)
household_size_levels <- c("1", "2", "3", "4", "5+")
redemption_rates$income <- factor(redemption_rates$income, levels = income_levels)


redemption_rates$household_size <- factor(redemption_rates$household_size, levels = household_size_levels)

ggplot(redemption_rates, aes(x = income, y = household_size, fill = redemption_rate)) +
  geom_tile(color = "white") +
  scale_fill_gradient(name = "Redemption Rate", low = "#D7EAF5", high = "#08306B") +
  labs(
    title = "Coupon Redemption Rates by Income Level and Household Size",
    subtitle = "Exploring the interaction between income and household size on coupon usage",
    x = "Household Income Level",
    y = "Household Size",
    caption = "Data Source: completejourney package"
  ) +
  theme_minimal(base_size = 12) +
  theme(
    plot.title = element_text(face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )