library(tidyverse)
library(completejourney)
products %>%
  filter(str_detect(product_category, regex("(FROZEN)|(FRZN)", ignore_case = TRUE))) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(age, home_ownership) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  mutate(home_ownership = fct_na_value_to_level(home_ownership, level = "Unknown")) %>%
  ggplot(aes(x = home_ownership, y = total_sales)) +
  geom_col() +
  facet_wrap(~ age) +
  scale_x_discrete("Home Ownership Status") +
  scale_y_continuous("Total Frozen Food Sales", labels = scales::dollar) +
  ggtitle("Who's Buying Frozen Food?",
          subtitle = "Homeowners ages 35-54 purchase the most frozen food.")

transactions_sample %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(income, household_size) %>%
  summarize(total_sales = sum(sales_value)) %>%
  arrange(desc(total_sales)) %>%
  ggplot(aes(x = total_sales , y = income , fill = household_size)) +
  geom_bar(stat = "identity") +
  guides(fill = guide_legend(title = "Household Size")) +
  scale_x_continuous("Total Sales", labels = scales::dollar) +
  scale_y_discrete("Customers' Income Range") +
  ggtitle("Customer Sales by Income Range and Household Size",
          subtitle = "Customers with an income range of $50-$74K and a household size of 2 have 
the highest total sales.")
## `summarise()` has grouped output by 'income'. You can override using the
## `.groups` argument.

products %>%
  filter(str_detect(product_category, regex("BREAD", ignore_case = TRUE))) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  mutate(day = lubridate::wday(transaction_timestamp, label = TRUE)) %>%
  group_by(day) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = day, y = total_sales)) +
  geom_col() +
  scale_x_discrete("Weekday") +
  scale_y_continuous("Total Bread and Bread Product Sales", labels = scales::dollar) +
  ggtitle("Which Day of the Week Do Customers Purchase the Most Bread?",
          subtitle = "Saturday and Sunday are the most common days customers purchase bread and bread products.")