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(ggplot2)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
library(naniar)
library(outliers)
transactions <- get_transactions()
promotions <- get_promotions()

combined_df <- 
  mutate(products, prod_cat_fct = as.factor(products$product_category)) %>%
  right_join(transactions, by = "product_id") %>%
  right_join(demographics, by =  "household_id")
combined_df %>%
  select(product_category, sales_value, income, brand) %>%
  filter(str_detect(product_category, "SOFT DRINK")) %>%
  ggplot(aes(brand, fill = brand)) +
  geom_bar() +
  facet_wrap(~ income) +
  labs(title = "Soft Drink Sales Based on Household Income Levels",
       subtitle = "Brand type preferences between household income levels",
       y = "Sum of Sales ($)",
       x = "Brand Label",
       fill = "Brand Label") +
  scale_y_continuous(labels = scales::dollar)

combined_df %>%
  group_by(income, product_category) %>%
  filter(str_detect(product_category, "COUPON/MISC ITEMS", negate = TRUE)) %>%
  summarise(total_cat_sales = sum(sales_value, na.rm = TRUE)) %>%
  arrange(income, desc(total_cat_sales), .by_group = TRUE) %>%
  slice_head(n = 3)%>%
  ggplot(aes(product_category, total_cat_sales, fill = product_category)) +
  geom_col() +
  facet_wrap(~ income) +
  ggtitle("Top Three Product Categories in Each Income Bracket") +
  scale_y_continuous(name = "Sum of Sales ($)", labels = scales::dollar) +
  scale_x_discrete("Product Category", labels = NULL) +
  labs(subtitle = "Based on Total Sales",
       fill = "Product Category")
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.

combined_df %>%
  mutate(combined_df,
         month = month(combined_df$transaction_timestamp)) %>%
  group_by(month, product_type) %>%
  filter(str_detect(product_category, "COUPON/MISC ITEMS", negate = TRUE)) %>%
  summarise(total_sold = sum(quantity, na.rm = TRUE), .groups = "drop_last") %>%
  arrange(month, desc(total_sold), .by_group = TRUE) %>%
  slice_head(n = 5) %>%
  ggplot(aes(month,total_sold, fill = product_type)) +
  geom_col(position = "stack") +
  ggtitle("Five Most Popular Products Each Month") +
  scale_y_continuous(name = "Quantity Sold") +
  scale_x_continuous(name = "Months", breaks = seq(1, 12, 1)) +
  labs(subtitle = "Based on Sales Volume",
       fill = "Product Category")