Package Requirements

Visual One

Ice Bought by Age Group

joined_data <- transactions %>%
  left_join(demographics, by = "household_id") %>%
  left_join(products, by = "product_id")

joined_data <- joined_data %>%
  filter(!is.na(age))

ice_by_age <- joined_data %>%
  filter(product_category == "FRZN ICE")

age_ice_quantity <- ice_by_age %>%
  group_by(age) %>%
  summarise(total_quantity = sum(quantity))

ggplot(age_ice_quantity, aes(x = age, y = total_quantity, group = 1)) +
  geom_line(color = "red") +
  geom_point(color = "black", size = 2) +
  labs(title = "Ice Bought by Age Group",
       x = "Age Group",
       y = "Quantity of Ice")

Visual Two

Product category by hour of the day

top_departments <- transactions %>%
  left_join(products, by = "product_id") %>%
  group_by(department) %>%
  summarize(total_quantity = sum(quantity), .groups = "drop") %>%
  top_n(5, wt = total_quantity) %>%
  pull(department)

vis2_data <- transactions %>%
  left_join(products, by = "product_id") %>%
  mutate(hour = hour(transaction_timestamp)) %>%
  group_by(hour, department) %>%
  summarise(quantity = sum(quantity), .groups = "drop") %>%
  filter(department %in% top_departments, hour >= 5, hour <= 23)

ggplot(vis2_data, aes(x = factor(hour), y = quantity, fill = department)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(name = "Number of products sold", labels = scales::comma) +
  labs(title = "Products Sold by Hour of the Day (Top 5 Departments)",
       x = "Hour of the Day",
       y = "Quantity Sold",
       fill = "Department") +
  theme_minimal() +
  theme(legend.position = "bottom",
        legend.text = element_text(size = 8))

Visual Three

Coupon Redemption by Household Income

intersect(colnames(coupon_redemptions), colnames(demographics))
## [1] "household_id"
visual3_joined_data <- coupon_redemptions %>%
  left_join(demographics, by = "household_id") %>%
  filter(!is.na(redemption_date), !is.na(income)) %>%
  group_by(income) %>%
  summarize(redemptions = sum(!is.na(redemption_date)))

ggplot(visual3_joined_data, aes(x = income, y = redemptions)) +
  geom_bar(stat = "identity", fill = "skyblue", color = "white") +
  labs(
    title = "Coupon Redemptions by Income Level",
    x = "Income Level",
    y = "Total Coupon Redemptions"
  ) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))