library(tidyverse)
library(completejourney)

Visualization 1

transactions <- transactions_sample

campaign_data <- campaigns %>%
  inner_join(campaign_descriptions)

transactions %>%
  inner_join(demographics) %>%
  inner_join(campaign_data) %>%
  select(c('campaign_type', 'sales_value', 'age')) %>%
  group_by(age, campaign_type) %>%
  summarize(total_sales = sum(sales_value, na.rm = TRUE)) %>%
  ggplot(aes(x = age, y = total_sales, fill = campaign_type)) +
    geom_bar(stat = "identity", position = "dodge") +
    scale_y_continuous(name = "Total_sales_volume", labels = scales::dollar) +
    labs(title = "Total Sales for different Age groups w.r.t Campaign Type",
         subtitle = "This graph is for registered users only.",
       x = "Age_Groups",
       y = "Total Sales",
       color = "Campaign Type")

Visualization 2

test  <- campaign_data %>%
  inner_join(demographics) %>%
  inner_join(transactions)

custom_colors <- c("National" = "dodgerblue", "Private" = "firebrick")

test %>%
  inner_join(products) %>%
  mutate(Month = lubridate::month(transaction_timestamp, label = TRUE)) %>%
  select(c('brand', 'Month', 'sales_value')) %>%
  group_by(Month, brand) %>%
  summarize(total_sales = sum(sales_value)) %>%
  ggplot(aes(x = Month, y = total_sales, color = brand, group = brand)) +
    geom_line(size = 1.2) +
    scale_color_manual(values = custom_colors) +
    scale_y_continuous(name = "Total_sales", labels = scales::dollar_format(prefix = "$")) +  # Add dollar sign
    labs(title = "Total Monthly Level Sales for each Brand",
         subtitle = "This graph shows the monthly sales trendline for different Brands",
       x = "Month",
       y = "Total Sales",
       color = "Brand") +
    theme_minimal() +
    theme(
      legend.position = "top",  # Move legend to the top
      legend.title = element_blank(),  # Remove legend title
      axis.title.x = element_text(size = 12),  # Increase x-axis label size
      axis.title.y = element_text(size = 12),  # Increase y-axis label size
      plot.title = element_text(size = 14, hjust = 0.5),  # Increase title size and center it
      axis.text.x = element_text(size = 10),  # Increase x-axis tick label size
      axis.text.y = element_text(size = 10),  # Increase y-axis tick label size
      panel.grid.major = element_line(color = "gray", size = 0.2),  # Remove major gridlines
      panel.grid.minor = element_blank()   # Remove minor gridlines
    )

Visualization 3

promotions <- promotions_sample 

transactions %>%
  inner_join(products) %>%
  inner_join(promotions) %>%
  select(c('product_category', 'mailer_location', 'sales_value')) %>%
  group_by(mailer_location, product_category) %>%
  summarize(avg_sales = mean(sales_value)) %>%
  arrange(desc(avg_sales)) %>%
  head(10) %>%
  ggplot(aes(x = mailer_location, y = avg_sales, fill = product_category)) +
    geom_bar(stat = "identity", position = "dodge", width = 0.7, color = "white") +
    labs(title = "Top 10 Average Sales by Mailer Location and Product Category",
         subtitle = "This graph shows Top 10 avg sales by Mailer Location and further provides a view of Product Category distribution in those Mailer Locations.",
       x = "Mailer Location",
       y = "Average Sales",
       fill = "Product Category") +
    scale_y_continuous(name = "Average_sales_volume", labels = scales::dollar) +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 45, hjust = 1),  # Rotate x-axis labels
        legend.title = element_text(size = 12, face = "bold"),  # Customize legend title
        legend.text = element_text(size = 10),  # Customize legend text
        plot.title = element_text(size = 16, hjust = 0.5, face = "bold"),  # Title size and alignment
        axis.title.x = element_text(size = 12, face = "bold"),  # X-axis title size and style
        axis.title.y = element_text(size = 12, face = "bold"))  # Y-axis title size and style