library(tidyverse)
library(completejourney)

First Visualization:

transactions <- get_transactions() %>% 
  mutate(Week = week(transaction_timestamp))
households <- demographics
promotions <- get_promotions()

wkly_avg <- transactions %>% 
  inner_join(products) %>% 
  semi_join(promotions, by = "product_id") %>% 
  group_by(Week, product_category) %>%
  summarise(week_qty = sum(quantity)) %>% 
  group_by(product_category) %>% 
  summarise(avg_qty = mean(week_qty))
campaign_simple <- campaign_descriptions %>% 
  inner_join(coupons) %>% 
  select(product_id, campaign_id, start_date, end_date) 
campaign_wkly_avg <- transactions %>% 
  select(product_id, Week, quantity) %>% 
  inner_join(campaign_simple) %>% 
  inner_join(products) %>% 
  select(product_id, Week, product_category, quantity, start_date, end_date) %>% 
  group_by(Week, product_category) %>% 
  summarise(camp_week_qty = sum(quantity)) %>% 
  ungroup() %>% 
  group_by(product_category) %>% 
  summarise(camp_avg_qty = mean(camp_week_qty))
campaign_data <- campaign_wkly_avg %>% 
  inner_join(wkly_avg) %>% 
  mutate(diff = camp_avg_qty - avg_qty) %>% 
  mutate(product_category = fct_reorder(product_category, desc(diff))) %>% 
  arrange(desc(diff)) %>% 
  top_n(n = 10, wt = diff) 
ggplot(campaign_data, aes(x = product_category, y = diff)) +
  geom_col() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        panel.background = element_rect(fill = "grey")) +
  xlab("Product Category") +
  ylab("Increase in Sales Quantity") +
  ggtitle("Relationship between Sales and Promotions of the Top 10 Product Category") +       labs(subtitle = "Sales and Promotions of Top 10 Product Category")

  geom_tile(color = 'white')
## geom_tile: linejoin = mitre, na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity

Second Visualization:

filtered_products_fruit_bread <- products %>%
  filter(str_detect(product_category, regex("(FRUIT)|(BREAD)", ignore_case = TRUE))) %>%
  inner_join(transactions_sample, by = "product_id") %>%
  inner_join(demographics, by = "household_id") %>%
  group_by(income) %>%
  summarise(total_sales = sum(sales_value, na.rm = TRUE))
  
ggplot(filtered_products_fruit_bread, aes(x = income, y = total_sales)) +
  geom_point() +
  scale_x_discrete("Income Level") + 
  scale_y_continuous("Total Sales", labels = scales::dollar) +
  ggtitle("Comparison of Fruit and Bread Sales by Income Level") +
  labs(subtitle = "Relationship between the Sales of Fruit and Bread by Income Level")

Third Visualization:

transactions %>%
  inner_join(demographics) %>% 
  filter(marital_status == "Unmarried") %>%
  ggplot(aes(x = quantity, y = sales_value, color = income)) +
  geom_line() +
  scale_y_continuous(name = "Total Sales Value", labels = scales::dollar) +
  scale_x_log10(name = "Total Sales Volume") +
  labs(
    title = "Volume of Sales of Unmarried People",
    subtitle = "Total sales of unmarried people shown with different income ranges."
  ) +
  theme_minimal() + 
  scale_color_brewer(palette = "green")