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.3     ✔ 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(completejourney)
## Welcome to the completejourney package! Learn more about these data
## sets at http://bit.ly/completejourney.
library(dplyr)
library(ggplot2)

coupon_redemptions %>%
  inner_join(coupons, by = "coupon_upc") %>%
  inner_join(products, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(household_id, coupon_upc) %>%
  summarise(total_coupons = n()) %>%
  group_by(household_id) %>%
  summarise(total_coupons_per_household = sum(total_coupons)) %>%
  arrange(desc(total_coupons_per_household)) %>%
  top_n(10, total_coupons_per_household) %>%
  ggplot(aes(x = reorder(as.factor(household_id), -total_coupons_per_household), 
             y = total_coupons_per_household, group = 1)) +
  geom_line(color = "green", size = 1) +
  geom_point(color = "black", size = 3) +
  scale_x_discrete("Household Identifier") +
  scale_y_continuous("Total Coupons Redeemed Across Products") +
  ggtitle("Top 10 Households by Coupon Usage Across Products",
          subtitle = "Line chart illustrating total coupons redeemed for each household.")
## Warning in inner_join(., coupons, by = "coupon_upc"): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 1 of `x` matches multiple rows in `y`.
## ℹ Row 91127 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
## `summarise()` has grouped output by 'household_id'. You can override using the
## `.groups` argument.
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

library(ggplot2)
library(dplyr) 
library(completejourney)
library(tidyverse)
library(lubridate)

transactions_sample %>%
  inner_join(products) %>%
  inner_join(demographics) %>%
  filter(product_category == "SUSHI") %>%
  group_by(income) %>%
  summarize(total_sushi_purchases = sum(quantity, na.rm = TRUE)) %>%
  ggplot(aes(x = income, y = total_sushi_purchases, fill = total_sushi_purchases)) + 
  geom_col() + 
  labs(
    title = "Total Sushi Purchases by Income Level",
    subtitle = "Hypothesis: Customers with higher incomes (150k+) are expected to \n purchase more sushi due to its premium price.",
    x = "Annual Income Levels ($)",
    y = "Number of Sushi Purchases") +  
  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)`
## Joining with `by = join_by(household_id)`

library(tidyverse)
library(completejourney)
library(dplyr)
library(ggplot2)

products %>% 
  filter(str_detect(product_category, regex("(SODA)|(JUICE)|(WATER)", ignore_case = TRUE))) %>% 
  inner_join(transactions_sample, by = "product_id") %>% 
  inner_join(demographics, by = "household_id") %>% 
  group_by(income, age) %>% 
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>% 
  mutate(age = fct_na_value_to_level(age, level = "Unknown")) %>% 
  ggplot(aes(x = age, y = total_sales)) +
  geom_col(fill = "green") +
  facet_wrap(~ income) +
  scale_x_discrete("Age Category") +
  scale_y_continuous("Total Beverage Sales", labels = scales::dollar) +
  ggtitle("Purchases Beverages by Age Group and Income?",
          subtitle = "The age group from 35-44 with an income of $50-74K buys the most beverages.")
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.