category_spend <- completejourney::transactions_sample %>%
  inner_join(products) %>%
  group_by(product_category) %>%
  mutate(product_category = fct_lump(product_category, 10)) %>%
  summarise(total_spend = sum(sales_value), .groups = "drop")%>%
  arrange(desc(total_spend))%>%
  slice(2:11)

category_spend = category_spend %>%
  mutate(product_category = recode(product_category, 
                                   "FRZN MEAT/MEAT DINNERS" = "FROZEN MEALS",
                                   "BAKED BREAD/BUNS/ROLLS" = "BAKED GOODS",
                                   "FLUID MILK PRODUCTS" = "MILK PRODUCTS",
                                   "BEERS/ALES" = "BEERS & ALES"))

ggplot(category_spend, aes(x = reorder(product_category, -total_spend), y = total_spend)) +
  geom_bar(stat = "identity", aes(fill = total_spend), show.legend = FALSE) +
  scale_fill_gradient(high = "orchid", low = "#BF94E4") +
  geom_text(aes(label = scales::label_dollar(accuracy = 1)(total_spend)), hjust = 1.2, color = "white", size = 3) +
  labs(title = "Revenue by Product Categories", 
       subtitle = "Total Revenue for the Top 10 product categories sold.",
       caption = "Data Source: CompleteJourney\nAnalysis by Kaitlyn Straight",
       x = "Product Category", y = "Total Revenue (USD)") +
  theme_classic() +
  theme(axis.text.x = element_text(hjust = 1),
        plot.title = element_text(hjust = 0, size = 16, face = "bold"),
        axis.title = element_text(size = 12, face = "bold"),
        panel.grid.major.x = element_line(color = "gray90"),
        panel.grid.minor.y = element_line(color = "gray40", linetype = "dotted")) + 
  scale_y_continuous(labels = scales::label_dollar(), 
                     expand = c(-.1, max(category_spend$total_spend) * 0.1)) +
  coord_flip() 

product_cat_months <- transactions_sample %>%
  inner_join(products) %>%
  mutate(month = floor_date(transaction_timestamp, "month")) %>%
  group_by(month, product_category) %>%
  summarise(total_spend = sum(sales_value, na.rm = TRUE), .groups = "drop") %>%
  mutate(product_category = fct_reorder(product_category, total_spend))


top_5_categories <- product_cat_months %>%
  group_by(product_category) %>%
  summarise(total_spend = sum(total_spend), .groups = "drop") %>%
  arrange(desc(total_spend))%>%
  slice(2:6) %>%
  pull(product_category)
product_cat_months_top5 <- product_cat_months %>%
  filter(product_category %in% top_5_categories)

product_cat_months_top5 = product_cat_months_top5 %>%
  mutate(product_category = recode(product_category, 
                                   "FRZN MEAT/MEAT DINNERS" = "FROZEN MEALS",
                                   "FLUID MILK PRODUCTS" = "MILK PRODUCTS",
                                   "BEERS/ALES" = "BEERS & ALES"))

chosen_palette = wes_palette("Zissou1", n = 5)

product_cat_months_top5 %>%
  ggplot(aes(x = month, y = total_spend, color = product_category)) +
  geom_line(size = 2, alpha = 0.5) + 
  geom_point(size = 3, alpha = 0.5) + 
  geom_smooth(method = "lm", se = FALSE, aes(group = product_category), linetype = "dashed", size = 1) +
  labs(title = "Revenue Over Time for Product Categories",    
       subtitle = "Revenue by month for Top 5 product categories sold.",
       caption = "Data Source: CompleteJourney\nAnalysis by Kaitlyn Straight",
       x = "Month", y = "Total Revenue (USD)") +
  scale_color_manual(values = chosen_palette) +
  scale_x_date(labels = scales::date_format("%b %Y"), breaks = "1 month") +  
  scale_y_continuous(labels = scales::label_dollar()) +
  theme_classic() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
    axis.title = element_text(size = 12, face = "bold"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10),
    panel.grid.major = element_line(color = "gray90", size = 0.5),
    panel.grid.minor = element_line(color = "gray95", size = 0.5),
    plot.caption = element_text(face = "italic")  
    )
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

income_kids_coupons <- transactions_sample %>%
  inner_join(demographics) %>%
  mutate(Kid_Status = ifelse(kids_count > 0, TRUE, FALSE)) %>%
  group_by(Kid_Status, income) %>%
  summarise(total_coupons = sum(retail_disc, na.rm = TRUE) + 
              sum(coupon_disc, na.rm = TRUE) + 
              sum(coupon_match_disc, na.rm = TRUE), .groups = "drop")

palette_colors = c("darkgreen", "palegreen3")

ggplot(income_kids_coupons, aes(x = income, y = total_coupons, fill = Kid_Status)) +
  geom_bar(stat = "identity", show.legend = TRUE, position = "dodge", color = "palegreen4") +
  scale_fill_manual(values = palette_colors,
                    labels = c("No Kids", "Kids")) +
  geom_text(aes(label = scales::label_dollar(accuracy = 1)(total_coupons)),
            position = position_dodge(0.9),
            hjust = -0.1, vjust = 0.4,
            color = "grey20", size = 2.5, fontface = "bold") +
  labs(title = "Coupons Used by Income Level",
       subtitle = "USD used in coupons by income level, faceted by kid status.",
       caption = "Data Source: CompleteJourney\nAnalysis by Kaitlyn Straight",
       x = "Income", y = "Total Coupons Used (USD)") +
  theme_bw() +
  theme(
    axis.text.x = element_text(hjust = 1, angle = 45),
    plot.title = element_text(hjust = 0, size = 16, face = "bold"),
    axis.title = element_text(size = 12, face = "bold"),
    panel.grid.major.x = element_line(color = "gray90"),
    panel.grid.minor.y = element_line(color = "gray40", linetype = "dotted"),
    plot.caption = element_text(face = "italic")
  ) +
  scale_y_continuous(labels = scales::label_dollar(),
                     expand = expansion(mult = c(0, 0.1)),
                     n.breaks = 10) +
  coord_flip()