library(tidyverse)
library(completejourney)
transactions <- transactions_sample

##Plot 1: Top 3 products for 19-24 year old clients

transactions_sample %>%
  inner_join(demographics, by = "household_id")%>%
  filter(age == "19-24") %>%
  inner_join(products, by= "product_id") %>%
  group_by(product_id, product_type)%>%
  summarize(total_sales = sum(sales_value, na.rm= TRUE))%>%
  arrange(desc(total_sales))%>%
  head(n=3)%>%
  ggplot(aes(x = product_type, y = total_sales)) + 
  geom_bar(stat = "identity") +
  scale_y_continuous(name = "Sales", labels = scales::dollar) +
  scale_x_discrete(name = "Product Type") +
  labs(
  title = "Top 3 products for 19-24 year old clients",
  subtitle= "Gasoline is the most purchased product"
)+
  theme_light()

##Plot 2: How was the consumption of PB by households with income 50-74K?

products %>%
  filter(str_detect(product_type, regex("peanut butter", ignore_case = TRUE))) %>%
  inner_join(transactions, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  filter(income == "50-74K") %>%
  mutate(month = month(transaction_timestamp, label = TRUE)) %>%
  group_by(month) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = month, y = total_sales)) +
  geom_jitter(size = 3) +  # Use geom_line for line plot
  labs(title = "2017 Sales of Peanut Butter by 50-74K households",
       subtitle = "June was the month with most sales",
       x = "Month",
       y = "Total Sales") +
  theme_minimal()

##Plot 3: which day of the week pizza items are most frequently bought?

products %>%
  filter(str_detect(product_category, regex("(pizza)", ignore_case = TRUE))) %>%
  inner_join(transactions,"product_id") %>%
  inner_join(demographics, "household_id") %>%
  mutate(day_of_week = wday(transaction_timestamp, label = TRUE)) %>%
  group_by(day_of_week) %>%
  summarize(total_purchases = n()) %>%
  ggplot(aes(x = day_of_week, y = total_purchases)) +
  geom_boxplot(size = 3) +
  labs(title = "Pizza Consumption by Day of Week", subtitle = "Sundays and Saturdays are the days when pizza items are most consumed") +
  scale_y_continuous(name = "Total Pizza Purchases", labels = scales::dollar) +
  scale_x_discrete(name = "Day of Week") +
  theme_bw()