# Which age and income spends the most on cookies?

products %>%
  filter(str_detect(product_category, "COOKIES"), ignore_case = TRUE) %>%
  filter(str_detect(product_type, "COOKIES"), ignore_case = TRUE) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(age, income) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = age, y = total_sales)) +
  geom_col(fill = "PINK") +
  theme(axis.text.x = element_text(size = 8, angle = 55))  +
  facet_wrap(~ income) +
  scale_x_discrete("Age") +
  scale_y_continuous("Total Cookie Sales", labels = scales::dollar) +
  ggtitle("Who's Buying Cookies?", 
          subtitle = "Ages 45-50 with an income of 50-99k are purchasing the most cookies.")
## `summarise()` has grouped output by 'age'. You can override using the `.groups`
## argument.

# What is the marital status and income of those who spend the most on cookies?

products %>%
  filter(str_detect(product_category, "COOKIES"), ignore_case = TRUE) %>%
  filter(str_detect(product_type, "COOKIES"), ignore_case = TRUE) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(marital_status, income) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  mutate(marital_status = fct_na_value_to_level(marital_status, level = "Unknown")) %>%
  ggplot(aes(x = marital_status, y = total_sales)) +
  geom_boxplot(color = "BLUE") +
  theme(axis.text.x = element_text(angle = 55)) +
  facet_wrap(~ income) +
  scale_x_discrete("Marital Status") +
  scale_y_continuous("Total Cookie Sales", labels = scales::dollar) +
  ggtitle("Do Married or Unmarried Households Buy More Cookies?", 
          subtitle = "Married households with an income of $35-74k purchase the most cookie products.")
## `summarise()` has grouped output by 'marital_status'. You can override using
## the `.groups` argument.

# Do married or unmarried households with kids buy more cookies than households without kids?

products %>%
  filter(str_detect(product_category, "COOKIES"), ignore_case = TRUE) %>%
  filter(str_detect(product_type, "COOKIES"), ignore_case = TRUE) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(kids_count, marital_status) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  drop_na() %>%
  ggplot(aes(x = kids_count, y = total_sales)) +
  geom_col(color = "PINK") +
  facet_wrap(~ marital_status) +
  scale_x_discrete("Number of Kids") +
  scale_y_continuous("Total Cookie Sales", labels = scales::dollar) +
  ggtitle("Do Married or Unmarried Households With or Without Kids Buy More Cookies?", 
          subtitle = "Umarried households with no kids purchase the most cookie products.")
## `summarise()` has grouped output by 'kids_count'. You can override using the
## `.groups` argument.

# Which household sizes spend the least and most on cookies?

products %>%
  filter(str_detect(product_category, "COOKIES"), ignore_case = TRUE) %>%
  filter(str_detect(product_type, "COOKIES"), ignore_case = TRUE) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(household_size, household_comp) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = household_size, y = total_sales)) +
    geom_col() +
  facet_wrap(~ household_comp) +
  scale_x_discrete("Household Size") +
  scale_y_continuous("Total Cookie Sales", labels = scales::dollar) +
  ggtitle("Which Households Sizes Buy Cookies?", 
          subtitle = "One Adult with Kids Spend the Least and Two Adults with No Kids spend the Most.")
## `summarise()` has grouped output by 'household_size'. You can override using
## the `.groups` argument.