This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#khai báo thư viện
library("utils")
library("readxl") #đọc excel
#khai báo thư viện của ggplot2
library(ggplot2)
library(tidyverse)
library(ggthemes)
#chorddiag
install.packages("chorddiag")
install.packages("devtools")
devtools::install_github("mattflor/chorddiag", build_vignettes = TRUE)
library(chorddiag)
#mặc định
library("reshape2")
library("knitr") # tạo bảng
library("grid")
library("gridExtra") # vẽ nhiều đồ thị cùng lúc
library("kableExtra") # tạo bảng
library("naniar") # cho dữ liệu missing
library("mice") # điền các giá trị missing
library("rmarkdown")
# bar chart race
library("gganimate")
library("ggflags")
library("countrycode")
#đồ thị
ggplot(data = os, aes(x = delivery_charges,y = customer_long, fill = is_happy_customer, col = is_happy_customer)) + geom_point() +
labs(x = "order price", y = "coupon discount", title = "gia tri hang hoa mua theo mua") +
geom_smooth(method = "lm")
#đồ thị bar chart race
p_ani <- os %>%
drop_na(delivery_charges) %>%
count(nearest_warehouse, date) %>%
arrange (date, desc(n)) %>%
group_by(date) %>%
#mutate(Noc_2 = tolower(countrycode(nearest_warehouse, "iso3c", "iso2c"))) %>%
drop_na() %>%
mutate(rank = rank(order(n, decreasing = T))) %>%
arrange(date, rank) %>%
filter(date %in% 1:12) %>%
ggplot(aes(x = rank, y = n)) +
geom_col(fill = "azure3", color = "black", width = 0.7) +
geom_text(aes(y = 0, label = date, size = 5)) +
geom_text(aes(x = rank, y = 0, label = nearest_warehouse), size = 3) +
coord_flip(expand = F, clip = "off") +
scale_x_reverse(limits = c(10.5 , 0)) +
labs(x = "", title = "", y = "delivery_charges") +
theme(
panel.background = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x = element_text(margin = margin(t = 20), face = "bold")
) +
transition_states(date, transition_length = 4, state_length = 1)
animate(p_ani, fps = 30, duration = 20, width = 700, height = 470, res = 120)
# order_price
ggplot(data = os, aes(x = order_price, y = delivery_charges, fill = gr_price))+ geom_boxplot() +
geom_jitter(alpha = 0.5) + theme_bw() + labs(x = "order_price", y = "delivery_charges") +
theme(axis.title.x = element_text(size = 10, margin = margin(t = 10)),
axis.title.y = element_text(size = 10, margin = margin(r = 10)),
plot.title = element_text(margin = margin(b = 10), face = "bold", hjust = 0.5, size = 12)
) + theme(legend.position = "none")
os$gr_price[os$order_price < 10000] = "A"
os$gr_price[os$order_price >= 10000 & os$order_price < 20000] = "B"
os$gr_price[os$order_price >= 20000] = "C"
# customer_lat
ggplot(data = os, aes(x = customer_lat, y = delivery_charges))+ geom_boxplot() +
geom_jitter(alpha = 0.5) + theme_bw() + labs(x = "customer_lat", y = "delivery_charges") +
theme(axis.title.x = element_text(size = 10, margin = margin(t = 10)),
axis.title.y = element_text(size = 10, margin = margin(r = 10)))
)
# customer_long
ggplot(data = os, aes(x = customer_long, y = delivery_charges))+ geom_boxplot() +
geom_jitter(alpha = 0.5) + theme_bw() + labs(x = "customer_long", y = "delivery_charges") +
theme(axis.title.x = element_text(size = 10, margin = margin(t = 10)),
axis.title.y = element_text(size = 10, margin = margin(r = 10)))
)
# order_total
os$gr_total[os$order_total < 10000] = "A"
os$gr_total[os$order_total >= 10000 & os$order_total < 20000] = "B"
os$gr_total[os$order_total >= 20000] = "C"
ggplot(data = os, aes(x = order_total, y = delivery_charges, fill = gr_total))+ geom_boxplot() +
geom_jitter(alpha = 0.5) + theme_bw() + labs(x = "order_total", y = "delivery_charges") +
theme(axis.title.x = element_text(size = 10, margin = margin(t = 10)),
axis.title.y = element_text(size = 10, margin = margin(r = 10)),
plot.title = element_text(margin = margin(b = 10), face = "bold", hjust = 0.5, size = 12)
) + theme(legend.position = "none")
# distance_to_nearest_warehouse
os$gr_dis[os$distance_to_nearest_warehouse < 1] = "A"
os$gr_dis[os$distance_to_nearest_warehouse >= 1 & os$distance_to_nearest_warehouse < 2] = "B"
os$gr_dis[os$distance_to_nearest_warehouse >= 2] = "C"
ggplot(data = os, aes(x = distance_to_nearest_warehouse, y = delivery_charges, fill = gr_dis))+ geom_boxplot() +
geom_jitter(alpha = 0.5) + theme_bw() + labs(x = "distance_to_nearest_warehouse", y = "delivery_charges") +
theme(axis.title.x = element_text(size = 10, margin = margin(t = 10)),
axis.title.y = element_text(size = 10, margin = margin(r = 10)),
plot.title = element_text(margin = margin(b = 10), face = "bold", hjust = 0.5, size = 12)
) + theme(legend.position = "none")