# Load required libraries
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.5.2
## Warning: package 'ggplot2' was built under R version 4.5.2
## Warning: package 'tibble' was built under R version 4.5.2
## Warning: package 'tidyr' was built under R version 4.5.2
## Warning: package 'readr' was built under R version 4.5.2
## Warning: package 'purrr' was built under R version 4.5.2
## Warning: package 'dplyr' was built under R version 4.5.2
## Warning: package 'stringr' was built under R version 4.5.2
## Warning: package 'forcats' was built under R version 4.5.2
## Warning: package 'lubridate' was built under R version 4.5.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.2.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidyquant)
## Warning: package 'tidyquant' was built under R version 4.5.2
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Warning: package 'xts' was built under R version 4.5.2
## Warning: package 'zoo' was built under R version 4.5.2
## Warning: package 'quantmod' was built under R version 4.5.2
## Warning: package 'TTR' was built under R version 4.5.2
## Warning: package 'PerformanceAnalytics' was built under R version 4.5.2
## ── Attaching core tidyquant packages ─────────────────────── tidyquant 1.0.11 ──
## ✔ PerformanceAnalytics 2.0.8      ✔ TTR                  0.24.4
## ✔ quantmod             0.4.28     ✔ xts                  0.14.1
## ── Conflicts ────────────────────────────────────────── tidyquant_conflicts() ──
## ✖ zoo::as.Date()                 masks base::as.Date()
## ✖ zoo::as.Date.numeric()         masks base::as.Date.numeric()
## ✖ dplyr::filter()                masks stats::filter()
## ✖ xts::first()                   masks dplyr::first()
## ✖ dplyr::lag()                   masks stats::lag()
## ✖ xts::last()                    masks dplyr::last()
## ✖ PerformanceAnalytics::legend() masks graphics::legend()
## ✖ quantmod::summary()            masks base::summary()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)

# Read the data from CSV file
bike_orderlines_wrangled_tbl <- read_csv("C:/Users/user/Downloads/bike_orderlines.csv")
## Rows: 15644 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): model, category_1, category_2, frame_material, bikeshop_name, city...
## dbl  (5): order_id, order_line, quantity, price, total_price
## dttm (1): order_date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Fix column names (replace dots with underscores)
names(bike_orderlines_wrangled_tbl) <- names(bike_orderlines_wrangled_tbl) %>% 
  str_replace_all("\\.", "_")

# Prepare data for faceted time series plot
sales_by_category2_time <- bike_orderlines_wrangled_tbl %>%
  select(order_date, category_2, total_price) %>%
  mutate(order_date = ymd(order_date)) %>%
  mutate(year_month = floor_date(order_date, unit = "month")) %>%
  group_by(year_month, category_2) %>%
  summarise(sales = sum(total_price), .groups = 'drop')

# Create the faceted plot
sales_by_category2_time %>%
  ggplot(aes(x = year_month, y = sales, color = category_2)) +
  
  # Add points and lines
  geom_point(size = 2) +
  geom_line(linewidth = 1) +
  
  # Facet by category_2
  facet_wrap(~ category_2, scales = "free_y", ncol = 3) +
  
  # Format y-axis
  scale_y_continuous(labels = scales::dollar_format(scale = 1e-6, suffix = "M")) +
  
  # Format x-axis to show years
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  
  # Apply theme
  theme_tq() +
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5),
    legend.position = "none",
    strip.background = element_rect(fill = "#2c3e50"),
    strip.text = element_text(color = "white", face = "bold", size = 10),
    panel.spacing = unit(1, "lines")
  ) +
  
  # Labels
  labs(
    title = "Sales Trends by Product Category (2011-2015)",
    subtitle = "Monthly sales across different bike categories",
    x = "order_date",
    y = "sales",
    caption = "Sales analysis by secondary product category"
  )

# Alternative: Show all categories in one color scheme
sales_by_category2_time %>%
  # Create color palette for each category
  mutate(category_2 = factor(category_2)) %>%
  ggplot(aes(x = year_month, y = sales)) +
  
  geom_point(aes(color = category_2), size = 1.5, show.legend = FALSE) +
  geom_line(aes(color = category_2), linewidth = 0.8, show.legend = FALSE) +
  
  facet_wrap(~ category_2, scales = "free_y", ncol = 3) +
  
  scale_y_continuous(labels = scales::dollar_format(scale = 1e-6, suffix = "M")) +
  scale_x_date(date_breaks = "1 year", date_labels = "%Y") +
  scale_color_manual(values = c(
    "Cross Country Race" = "#2C3E50",
    "Cyclocross" = "#E31A1C",
    "Elite Road" = "#FF7F00",
    "Endurance Road" = "#CAB2D6",
    "Fat Bike" = "#FB9A99",
    "Over Mountain" = "#A6CEE3",
    "Sport" = "#B2DF8A",
    "Triathalon" = "#1F78B4",
    "Trail" = "#33A02C"
  )) +
  
  theme_minimal() +
  theme(
    axis.text.x = element_text(angle = 0, hjust = 0.5, size = 8),
    strip.background = element_rect(fill = "#34495e", color = NA),
    strip.text = element_text(color = "white", face = "bold", size = 9),
    panel.grid.minor = element_blank(),
    panel.spacing = unit(1.2, "lines"),
    plot.title = element_text(face = "bold", size = 14),
    plot.subtitle = element_text(size = 10, color = "gray40")
  ) +
  
  labs(
    title = "Sales Trends by Product Category",
    x = "order_date",
    y = "sales"
  )