library(tidyverse)
## Warning: package 'tidyverse' 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.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
sales_data_raw <- read_csv("C:/Users/Gilang/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.
sales_data <- sales_data_raw %>%
  mutate(
    order.date = as.Date(order_date),
    year = year(order.date)
  ) %>%
  select(order.date, category_1, category_2, total_price)
total_quarterly <- sales_data %>%
  mutate(date_rounded = floor_date(order.date, unit = "quarter")) %>%
  group_by(date_rounded) %>%
  summarise(total_revenue = sum(total_price))

total_monthly <- sales_data %>%
  mutate(date_rounded = floor_date(order.date, unit = "month")) %>%
  group_by(date_rounded) %>%
  summarise(total_revenue = sum(total_price))

total_weekly <- sales_data %>%
  mutate(date_rounded = floor_date(order.date, unit = "week")) %>%
  group_by(date_rounded) %>%
  summarise(total_revenue = sum(total_price))
category_quarterly <- sales_data %>%
  mutate(date_rounded = floor_date(order.date, unit = "quarter")) %>%
  group_by(category_1, category_2, date_rounded) %>%
  summarise(total_revenue = sum(total_price)) %>%
  ungroup()
## `summarise()` has grouped output by 'category_1', 'category_2'. You can
## override using the `.groups` argument.
category_monthly <- sales_data %>%
  mutate(date_rounded = floor_date(order.date, unit = "month")) %>%
  group_by(category_1, category_2, date_rounded) %>%
  summarise(total_revenue = sum(total_price)) %>%
  ungroup()
## `summarise()` has grouped output by 'category_1', 'category_2'. You can
## override using the `.groups` argument.
category_weekly <- sales_data %>%
  mutate(date_rounded = floor_date(order.date, unit = "week")) %>%
  group_by(category_1, category_2, date_rounded) %>%
  summarise(total_revenue = sum(total_price)) %>%
  ungroup()
## `summarise()` has grouped output by 'category_1', 'category_2'. You can
## override using the `.groups` argument.
 p1 <- ggplot(total_quarterly, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 2, color = "#2c3e50") +
  geom_line(color = "#2c3e50") +
  geom_smooth(method = "loess", span = 0.3, color = "blue", se = FALSE) +
  scale_y_continuous(labels = dollar_format(scale = 1e-6, suffix = "M")) +
  theme_bw() +
  labs(title = "Total Sales: Quarterly Trends", x = "", y = "Revenue (USD)")

# Chart 2: Monthly Sales Trends 
p2 <- ggplot(total_monthly, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1.5, color = "#2c3e50") +
  geom_line(color = "#2c3e50", alpha = 0.3) +
  geom_smooth(method = "loess", span = 0.3, color = "blue", se = TRUE) +
  scale_y_continuous(labels = dollar_format(scale = 1e-6, suffix = "M")) +
  theme_bw() +
  labs(title = "Total Sales: Monthly Trends", x = "", y = "Revenue (USD)")

# Chart 3: Weekly Sales Trends 
p3 <- ggplot(total_weekly, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1, color = "#2c3e50", alpha = 0.6) +
  geom_smooth(method = "loess", span = 0.1, color = "blue", se = TRUE) +
  scale_y_continuous(labels = dollar_format(scale = 1e-6, suffix = "M")) +
  theme_bw() +
  labs(title = "Total Sales: Weekly Trends", x = "", y = "Revenue (USD)")
# SECTION 2: ROAD BIKE SALES 

# Filter for Road Bikes
road_qt <- category_quarterly %>% filter(category_1 == "Road")
road_mo <- category_monthly   %>% filter(category_1 == "Road")
road_wk <- category_weekly    %>% filter(category_1 == "Road")
# Chart 4: Road - Quarterly 
p4 <- ggplot(road_qt, aes(x = date_rounded, y = total_revenue)) +
  geom_line(color = "black") +
  geom_point(size = 1.5) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Road Sales: Quarterly", x = "", y = "Revenue")

# Chart 5: Road - Monthly 
p5 <- ggplot(road_mo, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1.5, alpha = 0.5) +
  geom_smooth(method = "loess", span = 0.3, color = "red", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Road Sales: Monthly", x = "", y = "Revenue")
# Chart 6: Road - Weekly 
p6 <- ggplot(road_wk, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1, alpha = 0.3) +
  geom_smooth(method = "loess", span = 0.1, color = "red", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Road Sales: Weekly", x = "", y = "Revenue")
# SECTION 3: MOUNTAIN BIKE SALES

mtn_qt <- category_quarterly %>% filter(category_1 == "Mountain")
mtn_mo <- category_monthly   %>% filter(category_1 == "Mountain")
mtn_wk <- category_weekly    %>% filter(category_1 == "Mountain")
# Chart 7: Mountain - Quarterly 
p7 <- ggplot(mtn_qt, aes(x = date_rounded, y = total_revenue)) +
  geom_line(color = "black") +
  geom_point(size = 1.5) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Mountain Sales: Quarterly", x = "", y = "Revenue")
# Chart 8: Mountain - Monthly (Page 7)
p8 <- ggplot(mtn_mo, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1.5, alpha = 0.5) +
  geom_smooth(method = "loess", span = 0.3, color = "#18BC9C", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Mountain Sales: Monthly", x = "", y = "Revenue")

# Chart 9: Mountain - Weekly (Page 8)
p9 <- ggplot(mtn_wk, aes(x = date_rounded, y = total_revenue)) +
  geom_point(size = 1, alpha = 0.3) +
  geom_smooth(method = "loess", span = 0.1, color = "#18BC9C", se = TRUE) +
  facet_wrap(~ category_2, ncol = 1, scales = "free_y") +
  scale_y_continuous(labels = dollar_format(scale = 1e-3, suffix = "K")) +
  theme_bw() +
  labs(title = "Mountain Sales: Weekly", x = "", y = "Revenue")
print(p1) # Total Quarterly
## `geom_smooth()` using formula = 'y ~ x'

print(p2) # Total Monthly
## `geom_smooth()` using formula = 'y ~ x'

print(p3) # Total Weekly
## `geom_smooth()` using formula = 'y ~ x'

print(p4) # Road Quarterly

print(p5) # Road Monthly
## `geom_smooth()` using formula = 'y ~ x'

print(p6) # Road Weekly
## `geom_smooth()` using formula = 'y ~ x'

print(p7) # Mountain Quarterly

print(p8) # Mountain Monthly
## `geom_smooth()` using formula = 'y ~ x'

print(p9) # Mountain Weekly
## `geom_smooth()` using formula = 'y ~ x'