library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.1     ✔ stringr   1.5.2
## ✔ ggplot2   4.0.0     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.1.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(lubridate)
library(scales)
## 
## Attaching package: 'scales'
## 
## The following object is masked from 'package:purrr':
## 
##     discard
## 
## The following object is masked from 'package:readr':
## 
##     col_factor
library(tidyquant)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo 
## ── 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()
## ✖ scales::col_factor()           masks readr::col_factor()
## ✖ scales::discard()              masks purrr::discard()
## ✖ 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
# Load libraries
library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(RColorBrewer)
library(zoo)
# Libraries
library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(RColorBrewer)
library(zoo)



bike_orderlines_wrangled <- read_csv("/Users/macstore/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.
library(zoo)  # for as.yearqtr

quarterly_sales <- bike_orderlines_wrangled %>%
  mutate(year_quarter = as.yearqtr(order_date)) %>%
  group_by(year_quarter) %>%
  summarise(total_sales = sum(total_price), .groups = "drop")
ggplot(quarterly_sales, aes(year_quarter, total_sales)) +
  geom_line(color = "#2C6BED", linewidth = 1.2) +
  geom_point(color = "#2C6BED", size = 2) +
  scale_y_continuous(labels = dollar_format()) +
  labs(
    title = "Total Sales",
    subtitle = "Quarterly Sales Trends",
    x = NULL,
    y = "Revenue (USD)"
  ) +
  theme_tq() +
  theme(
    plot.title = element_text(face = "bold"),
    axis.title.y = element_text(size = 10)
  )

monthly_sales <- bike_orderlines_wrangled %>% 
  mutate(year_month = floor_date(order_date, "month")) %>% 
  group_by(year_month) %>% 
  summarise(total_sales = sum(total_price), .groups = "drop")
ggplot(monthly_sales, aes(year_month, total_sales)) +
  geom_point(color = "black", size = 1.8, alpha = 0.8) +
  geom_smooth(
    method = "loess",
    color = "#2C6BED",
    fill = "grey70",
    linewidth = 1.2,
    se = TRUE
  ) +
  scale_y_continuous(labels = dollar_format()) +
  labs(
    title = "Total Sales",
    subtitle = "Monthly Sales Trends",
    x = NULL,
    y = "Revenue (USD)"
  ) +
  theme_tq() +
  theme(
    plot.title = element_text(face = "bold"),
    axis.title.y = element_text(size = 10)
  )
## `geom_smooth()` using formula = 'y ~ x'

quarterly_category_sales <- bike_orderlines_wrangled %>%
filter(category_1 == "Road") %>%
mutate(year_quarter = as.yearqtr(order_date)) %>%
group_by(year_quarter, category_2) %>%
summarise(total_sales = sum(total_price), .groups = "drop")
quarterly_category_sales %>%
filter(category_2 == "Elite Road") %>%
ggplot(aes(year_quarter, total_sales)) +
geom_line(color = "#2c3e50", linewidth = 1.2) +
geom_point(color = "#2c3e50", size = 2) +
scale_y_continuous(labels = dollar_format()) +
labs(x = NULL, y = NULL) +
theme_tq()

quarterly_category_sales %>%
filter(category_2 == "Endurance Road") %>%
ggplot(aes(year_quarter, total_sales)) +
geom_line(color = "red", linewidth = 1.2) +
geom_point(color = "red", size = 2) +
scale_y_continuous(labels = dollar_format()) +
labs(x = NULL, y = NULL) +
theme_tq()

library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(zoo)
library(RColorBrewer)

# Filter for Road bikes and aggregate by quarter and category_2
road_quarterly <- bike_orderlines_wrangled %>%
  filter(category_1 == "Road") %>%
  mutate(year_quarter = as.yearqtr(order_date)) %>%
  group_by(year_quarter, category_2) %>%
  summarise(revenue = sum(total_price), .groups = "drop") %>%
  arrange(year_quarter)

# Define categories order (FIXED NAMES)
road_quarterly <- road_quarterly %>%
  mutate(
    category_2 = factor(
      category_2,
      levels = c(
        "Elite Road",
        "Endurance Road",
        "Road Triathlon",
        "Cyclocross"
      )
    )
  )

# Create color palette
road_colors <- brewer.pal(4, "Blues")

# Plot with facets (matches PDF layout)
ggplot(road_quarterly, aes(x = year_quarter, y = revenue)) +
  geom_line(color = "#2c3e50", linewidth = 1.2) +
  geom_point(color = "#2c3e50", size = 2) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(), limits = c(0, NA)) +
  labs(title = "Sales By Category 2", x = "", y = "") +
  theme_tq() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  )

library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(RColorBrewer)

# Filter for Road bikes and aggregate by month
road_monthly <- bike_orderlines_wrangled %>%
  filter(category_1 == "Road") %>%
  mutate(year_month = floor_date(order_date, "month")) %>%
  group_by(year_month, category_2) %>%
  summarise(revenue = sum(total_price), .groups = "drop") %>%
  arrange(year_month)

# Factor categories (FIXED spelling)
road_monthly <- road_monthly %>%
  mutate(
    category_2 = factor(
      category_2,
      levels = c(
        "Elite Road",
        "Endurance Road",
        "Road Triathlon",
        "Cyclocross"
      )
    )
  )

# Create color palette
road_colors <- brewer.pal(4, "Blues")

ggplot(road_monthly, aes(x = year_month, y = revenue)) +
  geom_point(size = 1, alpha = 0.5, color = "#2c3e50") +
  geom_smooth(
    method = "loess",
    se = TRUE,
    fill = "#A8DADC",
    alpha = 0.3,
    linewidth = 1.2,
    color = "#2c3e50"
  ) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(), limits = c(0, NA)) +
  scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
  labs(title = "Sales By Category 2", x = "", y = "") +
  theme_tq() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  )
## `geom_smooth()` using formula = 'y ~ x'

library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(RColorBrewer)

# Filter for Road bikes and aggregate by week
road_weekly <- bike_orderlines_wrangled %>%
  filter(category_1 == "Road") %>%
  mutate(year_week = floor_date(order_date, "week")) %>%
  group_by(year_week, category_2) %>%
  summarise(revenue = sum(total_price), .groups = "drop") %>%
  arrange(year_week)

# Factor categories (FIXED spelling)
road_weekly <- road_weekly %>%
  mutate(
    category_2 = factor(
      category_2,
      levels = c(
        "Elite Road",
        "Endurance Road",
        "Road Triathlon",
        "Cyclocross"
      )
    )
  )

# Create color palette (reuse if already created)
road_colors <- brewer.pal(4, "Blues")

# Plot with facets (weekly view)
ggplot(road_weekly, aes(x = year_week, y = revenue)) +
  geom_point(size = 0.8, alpha = 0.4, color = "#2c3e50") +
  geom_smooth(
    method = "loess",
    se = FALSE,
    span = 0.15,
    linewidth = 1.2,
    color = "#2c3e50"
  ) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(), limits = c(0, NA)) +
  scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
  labs(title = "Sales By Category 2", x = "", y = "") +
  theme_tq() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  )
## `geom_smooth()` using formula = 'y ~ x'

library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(zoo)
library(RColorBrewer)

# Filter for Mountain bikes and aggregate by quarter
mountain_quarterly <- bike_orderlines_wrangled %>%
  filter(category_1 == "Mountain") %>%
  mutate(year_quarter = as.yearqtr(order_date)) %>%
  group_by(year_quarter, category_2) %>%
  summarise(revenue = sum(total_price), .groups = "drop") %>%
  arrange(year_quarter)

# Define mountain categories (ordered)
mountain_quarterly <- mountain_quarterly %>%
  mutate(
    category_2 = factor(
      category_2,
      levels = c(
        "Cross Country Race",
        "Trail",
        "Over Mountain",
        "Sport",
        "Fat Bike"
      )
    )
  )

# Create color palette
mountain_colors <- brewer.pal(5, "Blues")

# Plot with facets
ggplot(mountain_quarterly, aes(x = year_quarter, y = revenue)) +
  geom_line(color = "#2c3e50", linewidth = 1.2) +
  geom_point(color = "#2c3e50", size = 2) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(), limits = c(0, NA)) +
  labs(title = "Sales By Category 2", x = "", y = "") +
  theme_tq() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  )

library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(RColorBrewer)

# Filter for Mountain bikes and aggregate by month
mountain_monthly <- bike_orderlines_wrangled %>%
  filter(category_1 == "Mountain") %>%
  mutate(year_month = floor_date(order_date, "month")) %>%
  group_by(year_month, category_2) %>%
  summarise(revenue = sum(total_price), .groups = "drop") %>%
  arrange(year_month)

# Factor categories (ordered)
mountain_monthly <- mountain_monthly %>%
  mutate(
    category_2 = factor(
      category_2,
      levels = c(
        "Cross Country Race",
        "Trail",
        "Over Mountain",
        "Sport",
        "Fat Bike"
      )
    )
  )

# Create color palette (reuse if already created)
mountain_colors <- brewer.pal(5, "Blues")

# Plot with facets (monthly view)
ggplot(mountain_monthly, aes(x = year_month, y = revenue)) +
  geom_point(size = 1, alpha = 0.5, color = "#2c3e50") +
  geom_smooth(
    method = "loess",
    se = TRUE,
    fill = "#A8DADC",
    alpha = 0.3,
    linewidth = 1.2,
    color = "#2c3e50"
  ) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(), limits = c(0, NA)) +
  scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
  labs(title = "Sales By Category 2", x = "", y = "") +
  theme_tq() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  )
## `geom_smooth()` using formula = 'y ~ x'

library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(RColorBrewer)

# Filter for Mountain bikes and aggregate by month
mountain_monthly <- bike_orderlines_wrangled %>%
  filter(category_1 == "Mountain") %>%
  mutate(year_month = floor_date(order_date, "month")) %>%
  group_by(year_month, category_2) %>%
  summarise(revenue = sum(total_price), .groups = "drop") %>%
  arrange(year_month)

# Factor categories (ordered)
mountain_monthly <- mountain_monthly %>%
  mutate(
    category_2 = factor(
      category_2,
      levels = c(
        "Cross Country Race",
        "Trail",
        "Over Mountain",
        "Sport",
        "Fat Bike"
      )
    )
  )

# Create color palette (reuse if already created)
mountain_colors <- brewer.pal(5, "Blues")

# Plot with facets (monthly view)
ggplot(mountain_monthly, aes(x = year_month, y = revenue)) +
  geom_point(size = 1, alpha = 0.5, color = "#2c3e50") +
  geom_smooth(
    method = "loess",
    se = TRUE,
    fill = "#A8DADC",
    alpha = 0.3,
    linewidth = 1.2,
    color = "#2c3e50"
  ) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(), limits = c(0, NA)) +
  scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
  labs(title = "Sales By Category 2", x = "", y = "") +
  theme_tq() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    strip.text = element_text(face = "bold"),
    legend.position = "none"
  )
## `geom_smooth()` using formula = 'y ~ x'