library(tidyverse)
library(completejourney)

Plot 1: Who is buying the most milk?

products %>%
     filter(str_detect(product_category, regex("(MILK)", ignore_case = TRUE))) %>%
     inner_join(transactions_sample, by = "product_id") %>%
     inner_join(demographics, by = "household_id") %>%
     group_by(home_ownership, 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() +
     facet_wrap(~ home_ownership) +
     scale_x_discrete("Age of Customer") +
     scale_y_continuous("Total Milk Sales", labels = scales::dollar) +
     ggtitle("Who's Buying The Most Milk?",
              subtitle = "Ages 45-54 homewoners are buying the most milk.")

Plot 2: Who is buying the most juice?

products %>%
  filter(str_detect(product_category, regex("(JUICE)", ignore_case = TRUE))) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(age, home_ownership, na.rm = TRUE) %>%
  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() +
  facet_wrap(~ home_ownership) +
  scale_x_discrete("Age of Customer") +
  scale_y_continuous("Total Juice Sales", labels = scales::dollar) +
  ggtitle("Who's Buying The Most Juice?",
          subtitle = "Ages 45-54 homeowners are buying the most juice.")
## `summarise()` has grouped output by 'age', 'home_ownership'. You can override
## using the `.groups` argument.

Plot 3: Do families with more kids and more money buy the most toys?

products %>%
  filter(str_detect(product_category, regex("(TOYS)", ignore_case = TRUE))) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(kids_count, income) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  mutate(income = fct_na_value_to_level(income, level = "Unknown")) %>%
  ggplot(aes(x = income, y = total_sales)) +
  geom_col() +
  facet_wrap(~ kids_count) +
  scale_x_discrete("Income of Family") +
  scale_y_continuous("Total Amount of Money Spent on Toys", labels = scales::dollar) +
  ggtitle("Households With What Number of Kids and What Income Level are Buying the Most Toys?",
          subtitle = "Families making 35-49K a year, with 0 kids buy the most toys.")