library(tidyverse)
library(completejourney)

Plot 1: How does age and the number of children one has affect how much children’s cereal they buy?

products %>%
  filter(str_detect(product_type, "KIDS CEREAL")) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(age, kids_count) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = kids_count, y = total_sales)) + 
  geom_col() + 
  facet_wrap(~ age) + 
  scale_y_continuous("Total Sales for Children's Cereal", labels = scales::dollar) + 
  scale_x_discrete("Number of Children") + 
  ggtitle("Who's Buying the most Children's Cereal?", 
          subtitle = "Households with no kids ranging from 35-54 buy the most children's cereal.")

Plot 2: For how much are households of 34-54 year-olds with no children willing to buy children’s cereal?

products %>%
  filter(str_detect(product_type, "KIDS CEREAL")) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(age == "35-44" | age == "45-54") %>%
  filter(kids_count == 0) %>%
  ggplot(aes(x = sales_value, fill = age)) +
  geom_histogram(position = "dodge") + 
  scale_y_continuous("Frequency of Purchase", breaks = c(1,2,3,4,5,6,7,8,9,10)) + 
  scale_x_continuous("Value 35-54 Year Olds Pay for Children's Cereal", labels = scales::dollar) + 
  ggtitle("At what Price do Households, with No Children, Purchase Children's Cereal?", 
          subtitle = "The Majority of Households Buy Children's Cereal that is less than $3.50") + 
  labs(fill = "Age Range")

Plot 3: How does manufacturing in the low-price cereal market operate?

products %>%
  filter(str_detect(product_type, "KIDS CEREAL")) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  filter(sales_value < 3.50) %>%
  count(manufacturer_id) %>%
  mutate(pct = n / sum(n)) %>%
  ggplot(aes(pct, reorder(manufacturer_id, pct))) +
  geom_point() +
  labs(x = "Market Share of Total Sales Transactions", y = "Manufacturer ID Number") + 
  geom_segment(
    aes(x = 0, xend = pct, y = manufacturer_id , yend = manufacturer_id),
    size = 0.15) + 
  scale_x_continuous(labels = scales::percent_format(accuracy = 10)) +
  ggtitle("Which Low-Price Children's Cereal Manufacturer Sells the Most?", 
          subtitle = "Among children's ceral < $3.50, manufacturer 69 has the most transactions.")