This is Ngo Huu Truong’s Markdown document for the mid-term exam Student_ID: 110035100

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(readxl)
library(dplyr)
library(ggplot2)
library(esquisse)
library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
bike_orderlines <- read_excel("bike_orderlines.xlsx")
bike_orderlines$order_date <- factor(format(bike_orderlines$order_date,'%Y'))
year <- unique(bike_orderlines$order_date)
TP <- unique(bike_orderlines$category_2)
revenue <- c()
order_date <- c()
category_2 <- c()
for (i in 1: length(year)) {
  for (j in 1:length(TP)) {
    result <- 0 
    bike_orderlines2 <- dplyr::filter(bike_orderlines, year[i]==order_date, TP[j]==category_2)
    for (z in 1: length(bike_orderlines2$total_price))
      result <- result+bike_orderlines2$total_price[z]
    revenue <- c(revenue, result)
    category_2 <- c(category_2, TP[j])
  }
  
}
order_date <- c(year[1], year[1], year[1], year[1], year[1], year[1], year[1], year[1], year[1])
for (i in 2: length(year)) {
  for (j in 1: length(TP)) {
    order_date <- c(order_date, year[i])
  }
  
}
bike_orderlines <- data.frame(order_date, category_2, revenue)
#1
ggplot(data = bike_orderlines, aes(x = order_date, y = revenue, fill = category_2)) + 
  scale_fill_hue(direction = 1) +
  labs(y = "Revenue", title = "Revenue By Year") +
  theme_linedraw() +
  theme(legend.position = "bottom") +
  facet_wrap(vars(category_2), scales = "free_y") +
  geom_col() +
  geom_smooth(aes(group = 1), method = "lm", se = FALSE) +
  scale_y_continuous(labels = comma) +
  scale_y_continuous(labels = scales::dollar_format()) +
  theme(axis.title.x = element_text(size = 1)) +
  scale_size_continuous(order_date)
#2
bike_orderlines$order_date <- as.numeric(as.character(bike_orderlines$order_date))
ggplot(bike_orderlines) +
  aes(x = order_date, y = revenue, fill = category_2) +
  geom_area(size = 1.5) +
  scale_fill_hue(direction = 1) +
  labs(y = "Revenue", title = "Sales Over Year by Category 2", subtitle = "Sales Trending up", fill = "2nd Category") +
  theme_minimal() +
  scale_y_continuous(labels = comma) +
  scale_y_continuous(labels = scales::dollar_format())