title: “Homewor of day 2”
author: “Thin Pham”
output:
html_document:
code_download: true
code_folding: hide
highlight: pygments
theme: flatly
toc: true
toc_float: true
word_document:
toc: true

Questions for teacher

#Question 1: How to rename legend lalbes? Used scale_fill_discrete(name = “Dose”, labels = c(“A”, “B”, “C”)) but did not work #QAuestion 2: Why adding fill = my_color to ggplot(aes), the colors changed? # Questions 3: text unreadable? resolve?

Section 1

This is my final product for day 2

readxl::read_excel("E:\\UNIVERSITY\\R\\Data\\_data_pci_file_general_file_1588582407-Indicator_PCI2018_VN.xlsx", sheet = 1) -> cpi2018
library(dplyr)

colnames(cpi2018)[1] = "ten_tinh"
colnames(cpi2018)[3] = "cpi"
colnames(cpi2018)[2] = "ranking"
glimpse(cpi2018)
## Rows: 69
## Columns: 13
## $ ten_tinh                              <chr> "An Giang", "BRVT", "Bắc Giang",…
## $ ranking                               <dbl> 28, 21, 36, 60, 39, 15, 4, 20, 6…
## $ cpi                                   <dbl> 63.65373, 64.02277, 63.00917, 60…
## $ `CSTP 1: Gia nhập thị trường`         <dbl> 6.962630, 7.569573, 7.340731, 6.…
## $ `CSTP 2: Tiếp cận đất đai`            <dbl> 7.606571, 6.411787, 6.659523, 6.…
## $ `CSTP 3: Tính Minh bạch`              <dbl> 6.139910, 6.182615, 6.109393, 6.…
## $ `CSTP 4: Chi phí thời gian`           <dbl> 7.571786, 7.032722, 7.013678, 5.…
## $ `CSTP 5: Chi phí không chính thức`    <dbl> 7.081919, 6.374629, 6.158585, 5.…
## $ `CSTP 6: Cạnh tranh bình đẳng`        <dbl> 6.885238, 5.420545, 4.877264, 6.…
## $ `CSTP 7: Tính năng động`              <dbl> 6.224586, 5.492597, 5.546659, 4.…
## $ `CSTP 8: Dịch vụ Hỗ trợ doanh nghiệp` <dbl> 6.239887, 6.594096, 6.437852, 4.…
## $ `CSTP 9: Đào tạo lao động`            <dbl> 5.582859, 6.472886, 6.544525, 6.…
## $ `CSTP 10: Thiết chế pháp lý và ANTT`  <dbl> 6.042195, 6.370670, 5.896238, 6.…
#==============
# cleaning data
# Obimit not related observations

cpi2018 %>% 
  slice(1:63) %>% 
  mutate(ranking_text = as.character(ranking)) -> cpi2018
# add zero to ranking
library(stringr)
library(stringi)


cpi2018 %>% 
  mutate(ranking_adj = case_when(str_count(ranking_text) == 1 ~ str_c("0", ranking_text),
                                 TRUE ~ ranking_text)) -> cpi2018

# create ranking

#subset(cpi2018, select = -soluongkt) -> cpi2018


cpi2018 %>% 
  mutate(phanloai = case_when(cpi > 70 ~ "Excelent",
            cpi >= 65.4 ~ "Good",
            cpi >= 62.3 ~ "Fair",
            cpi >= 60.02 ~ "Mediorce",
            TRUE ~ "Poor")) -> cpi2018
cpi2018 %>% 
  mutate(color_use = case_when(phanloai == "Excelent" ~ "#5a3560",
                               phanloai == "Good" ~ "#39538e",
                               phanloai == "Fair" ~ "#348884",
                               phanloai == "Mediorce" ~ "#6dce75",
                               phanloai == "Poor" ~ "#fce81e"))  %>% 
  pull(color_use) -> color_use
    


# Draw column chart

library(ggplot2)

cpi2018 %>% 
  ggplot(aes(y = reorder(ten_tinh, + cpi), x = cpi, fill = color_use)) +
  geom_col(width = 0.95) +
  theme(axis.ticks = element_blank()) +
  theme(panel.grid = element_blank()) +
  theme(panel.background = element_blank()) +
  labs(title = "Viet Nam CPI Index 2018", subtitle = "R used for Data visualisation",
       caption = "Data source: https://pcivietnam.vn/du-lieu-pci") +
  theme(plot.title = element_text(face = "bold", size = 12)) +
  theme(plot.subtitle = element_text(size = 8, color = "gray40")) +
  theme(plot.caption = element_text(face = "italic", color = "gray40", hjust = 0.2)) +
  theme(axis.text.x = element_text(size = 3)) +
  theme(axis.text.y = element_text(size = 5)) +
  theme(axis.title.x = element_blank()) +
  theme(axis.title.y = element_blank()) +
  scale_x_continuous(expand = c(0, 0), limits = c(0, 80)) +
  geom_text(aes(label = round(cpi, 2)), size = 1, hjust = -0.3) +
  theme(axis.ticks.x = element_blank()) +
  theme(legend.title = element_blank())