library(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
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(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ✔ readr     2.1.5
## ── 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(lubridate) 

data("demographics")
data("transactions_sample")

merged_data <- transactions_sample %>%
  inner_join(demographics, by = "household_id")

summary_data <- merged_data %>%
  group_by(income, store_id) %>%
  summarise(total_spending = sum(sales_value, na.rm = TRUE)) %>%
  ungroup()
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.
top_stores <- summary_data %>%
  group_by(store_id) %>%
  summarise(total_revenue = sum(total_spending, na.rm = TRUE)) %>%
  arrange(desc(total_revenue)) %>%
  slice_head(n = 10)

top_summary_data <- summary_data %>%
  filter(store_id %in% top_stores$store_id)

plot <- ggplot(top_summary_data, aes(x = store_id, y = total_spending, fill = income)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Total Spending by Income Level in Top 10 Stores",
       subtitle = "Comparison of spending by income level for the top 10 stores",
       x = "Store ID",
       y = "Total Spending ($)",
       fill = "Income Level") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

print(plot)

data("coupon_redemptions")

merged_data <- coupon_redemptions %>%
  inner_join(demographics, by = "household_id")

coupon_summary <- merged_data %>%
  group_by(income) %>%
  summarise(total_redemptions = n()) %>%
  ungroup()

coupon_plot <- ggplot(coupon_summary, aes(x = income, y = total_redemptions, fill = income)) +
  geom_bar(stat = "identity") +
  labs(title = "Total Coupon Redemptions by Income Level",
       subtitle = "Comparison of coupon usage across different income",
       x = "Income Level",
       y = "Total Redemptions",
       fill = "Income Level") +
  theme_minimal()

print(coupon_plot)

merged_data <- coupon_redemptions %>%
  inner_join(demographics, by = "household_id") %>%
  mutate(redemption_date = as.Date(redemption_date), 
         day_of_week = wday(redemption_date, label = TRUE))

coupon_summary <- merged_data %>%
  group_by(day_of_week, income) %>%
  summarise(total_redemptions = n()) %>%
  ungroup()
## `summarise()` has grouped output by 'day_of_week'. You can override using the
## `.groups` argument.
scatter_plot <- ggplot(coupon_summary, aes(x = day_of_week, y = total_redemptions)) +
  geom_point(size = 3, alpha = 0.7, aes(color = income)) +
  labs(title = "Coupon Redemptions by Day of the Week by Income",
       subtitle = "Comparison by Income Level",
       x = "Day of the Week",
       y = "Total Coupon Redemptions",
       color = "Income Level") +
  theme_minimal() +
  theme(text = element_text(size = 12),
        legend.position = "right") +
  scale_x_discrete(limits = levels(coupon_summary$day_of_week)) +
  facet_wrap(~ income)

print(scatter_plot)