# -------------------------------
# Step 0: Load Required Packages
# -------------------------------
library(ggplot2)
library(dplyr)

# -------------------------------
# Step 1: Load the Sales Data
# -------------------------------
sales_data <- read.csv("monthly_sales.csv")

# Quick look at the first few rows
head(sales_data)

# Optional: Clean or preprocess if needed
sales_data_clean <- sales_data %>%
  group_by(month) %>%
  summarise(total_sales = sum(total_sales, na.rm = TRUE))

# -------------------------------
# Chart 1: Bar Chart - Sales by Product Category
# -------------------------------
ggplot(data = sales_data) +
  aes(x = product_category, y = total_sales) +
  geom_col() +
  labs(title = "Sales by Product Category",
       x = "Product Category",
       y = "Total Sales ($)")

# -------------------------------
# Chart 2: Line Chart - Monthly Sales Trends
# -------------------------------
ggplot(data = sales_data_clean) +
  aes(x = month, y = total_sales, group = 1) +
  geom_line(color = "steelblue", size = 1) +
  geom_point(color = "darkred", size = 2) +
  labs(title = "Monthly Total Sales",
       x = "Month",
       y = "Total Sales ($)")

# -------------------------------
# Chart 3: Scatter Plot - Advertising vs Sales
# -------------------------------
ggplot(data = sales_data) +
  aes(x = advertising_spend, y = total_sales) +
  geom_point() +
  labs(title = "Advertising Spend vs Sales Revenue",
       x = "Advertising Spend ($)",
       y = "Total Sales ($)")

# -------------------------------
# Chart 4: Histogram - Distribution of Order Values
# -------------------------------
ggplot(data = sales_data) +
  aes(x = order_value) +
  geom_histogram(bins = 20) +
  labs(title = "Distribution of Customer Order Values",
       x = "Order Value ($)",
       y = "Number of Orders")

# -------------------------------
# Chart 5: Box Plot - Sales by Region
# -------------------------------
ggplot(data = sales_data) +
  aes(x = region, y = monthly_sales) +
  geom_boxplot() +
  labs(title = "Sales Performance by Region",
       x = "Sales Region",
       y = "Monthly Sales ($)")

# -------------------------------
# Chart 6: Enhanced Bar Chart with Custom Styling
# -------------------------------
ggplot(data = sales_data) +
  aes(x = product_category, y = total_sales, fill = product_category) +
  geom_col() +
  labs(title = "Q4 Sales Performance by Product Category",
       subtitle = "Electronics leads with $2.3M in revenue",
       x = "Product Category",
       y = "Total Sales ($)",
       caption = "Data source: Sales Database") +
  theme_minimal() +
  theme(legend.position = "none")
