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

Plot 1: What days are the grocery stores more crowded?

transactions_sample %>%
  inner_join(products) %>%
  filter(department == "GROCERY") %>%
  mutate(day_of_week = wday(transaction_timestamp, label = TRUE)) %>%
  mutate(transaction_date = substring(transaction_timestamp, 1, 10)) %>%
  group_by(day_of_week, transaction_date) %>%
  summarize(purchase_count = sum(quantity)) %>%
  ggplot(aes(x = factor(day_of_week), y = purchase_count)) +
  geom_violin(fill = "green") +
  labs(
    title = "Grocery Department Items -- Violin Plot by Weekday",
    subtitle = "What days are the Grocery Stores more Crowded?",
    x = "Day of the Week",
    y = "Daily Purchase Count") +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  theme(plot.subtitle = element_text(hjust = 0.5, face = "italic"))
## Joining with `by = join_by(product_id)`
## `summarise()` has grouped output by 'day_of_week'. You can override using the
## `.groups` argument.

Plot 2: Which age group uses the most coupons?

## Visualization 2
demographics %>%
  inner_join(completejourney::transactions_sample, by = "household_id") %>%
  group_by(income, age) %>%
  summarize(total_coupon_disc = sum(coupon_disc, na.rm = TRUE)) %>%
  ggplot(aes(x = age, y = total_coupon_disc)) +  
  geom_col(fill = "cyan") +
  scale_y_continuous("Total Coupon Discount", labels = scales::dollar) +
  scale_x_discrete("Age Group") +  
  facet_wrap(~income) +
  ggtitle("Which age group uses the most coupons?",
          subtitle = "45-54 year olds with income in the range of 50-74k use the most coupons;
         Incomes > 175k rarely use coupons regardless of age")
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.

Plot 3: what age group buys the most prepared foods?

products %>%
  filter(str_detect(product_category, "PREPARED FOOD")) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(age, household_size) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = factor(age), y = total_sales,  fill = household_size)) +  
  geom_col() +
  guides(fill = guide_legend(title = "Household Size")) +
  scale_y_continuous("Total Sales for Prepared Foods", labels = scales::dollar) +
  scale_x_discrete("Age") +
  labs(title = "What age group buys the most prepared foods?")
## `summarise()` has grouped output by 'age'. You can override using the `.groups`
## argument.