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

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.5     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v 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("C:/Users/HP/Downloads/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)

#Exercise_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)
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.
## `geom_smooth()` using formula 'y ~ x'

#Exercise_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 = 2.0) +
  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())
## Scale for 'y' is already present. Adding another scale for 'y', which will
## replace the existing scale.