library(tidyverse)
library(completejourney)
library(forcats)
library(dplyr)
library(knitr)
transactions <- get_transactions()
promotions <- get_promotions()
products %>%
  filter(str_detect(product_category,regex("SOUP"))) %>%
  filter(str_detect(manufacturer_id,regex("69"))) %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(home_ownership,age) %>%
  mutate(home_ownership = fct_na_value_to_level(home_ownership, level = "Unknown")) %>%
  mutate(home_ownership = fct_collapse(home_ownership,
                                       "Renter" = c("Renter", "Probable Renter"),
                                       "Homeowner" = c("Homeowner", "Probable Homeowner")
                                       )) %>%
  summarise(total_sales = sum(sales_value)) %>%
  mutate(home_ownership = fct_relevel(home_ownership, "Homeowner","Unknown","Renter"))%>%
  ggplot(aes(x=home_ownership,y = total_sales)) +
  geom_col(color = "black", fill = "Blue") +
  facet_wrap("age") +
  scale_y_continuous("Total Soup Sales for Manufactuerer Sixty-Nine",labels = scales::dollar)+
  scale_x_discrete("Home Ownership")+
  ggtitle("Who is Buying the Most of Manufacturer Sixty-Nine's Soup",
          subtitle = "Homeowner's in the Age Range of 45-54") +
  theme(plot.title = element_text(size = 30, face = "bold")) +
  theme(plot.subtitle = element_text(size = 20, face = "italic")) +
  theme(axis.text = element_text(size = 15)) +
  theme(axis.title = element_text(size = 20)) +
  theme(strip.text = element_text(size = 15))

products %>%
  filter(str_detect(product_category,regex("COLD CEREAL"))) %>%
  filter(str_detect(product_type,regex("KIDS CEREAL"))) %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(kids_count,income) %>%
  summarise(quantity_purchased = sum(quantity)) %>%
  ggplot(aes(x = kids_count, y= quantity_purchased)) +
  geom_boxplot(color = "black", fill = "orange") +
  scale_y_continuous("Total Kids Cereal Sales",labels = scales::dollar)+
  scale_x_discrete("Kids per Household")+
  ggtitle("Which Household Type is Buying the Most Kids Cereal",
          subtitle = "Households with Zero Kids") +
  theme(plot.title = element_text(size = 30, face = "bold")) +
  theme(plot.subtitle = element_text(size = 20, face = "italic")) +
  theme(axis.text = element_text(size = 15)) +
  theme(axis.title = element_text(size = 20)) +
  theme(strip.text = element_text(size = 15))

products %>%
  filter(str_detect(product_category,regex("BEER"))) %>%
  filter(str_detect(manufacturer_id,regex("69"))) %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  mutate(month = month(transaction_timestamp)) %>%
  group_by(month,income) %>%
  summarise(total_sales = sum(sales_value)) %>%
  ggplot(aes(x = month, y = total_sales)) +
  geom_point(color="red",size = 3, shape = 18)+
  geom_smooth() +
  facet_wrap("income") +
  scale_y_continuous("Total Beer Sales for Manufacturer Sixty-Nine",labels = scales::dollar)+
  scale_x_continuous("Month of the Year", breaks = 1:12, labels = month.abb)+
  ggtitle("Which Income Range Spends the Most on Manufacturer Sixty-Nine's Beer per Month ",
          subtitle = "Individuals in the Income Range of 50-74k in July") +
  theme(plot.title = element_text(size = 30, face = "bold")) +
  theme(plot.subtitle = element_text(size = 20, face = "italic")) +
  theme(axis.text = element_text(size = 15)) +
  theme(axis.title = element_text(size = 20)) +
  theme(strip.text = element_text(size = 15))