Dữ liệu sử dụng: Sử dụng data năm 2018 từ https://pcivietnam.vn/du-lieu-pci
Mục tiêu: Tạo ra
barplot sao cho giống nhất với biểu đồ cột có tên “Vietnam CPI Index
2018” từ https://rpubs.com/chidungkt/1129363
#-----------------------
# Homework 2: PCI
# Author: Ngo Ngoc Minh
#-----------------------
#-------------------
# Preparation
#-------------------
library(tidyverse)
library(readxl)
library(viridis)
library(showtext)
rm(list = ls())
pci <- read_excel("C:\\bivnn_FTU\\Cased_R\\R15_day2\\pci2018.xlsx")
font_add_google("Roboto Condensed", "Roboto")
showtext_auto()
#------------------------
# Data Cleaning
#------------------------
#Select related columns (first 3)
pci <- pci %>%
select(c(1,2,3))
#Rename Columns
names(pci)[c(1, 2, 3)] <- c("tinh", "ranking", "cpi_index")
#Expand BR-VT
pci <- pci %>%
mutate(tinh = if_else(tinh == "BRVT", "Bà Rịa - Vũng Tàu", tinh))
#Remove NA in rank
pci <- pci %>%
filter(!is.na(ranking))
#Add 0 before 1-digit ranking
pci <- pci %>%
mutate(ranking_adj = sprintf("%02d", ranking))
#New Column with province & ranking
pci <- pci %>%
mutate(province_rank = str_c(tinh, ranking_adj, sep = " "))
#Arrange by ascending ranking and remove duplicate row
pci <- pci %>%
distinct(ranking, .keep_all = TRUE)
#Rounding the indexes
pci <- pci %>%
mutate(cpi_index_rounded = format(round(cpi_index, 2), nsmall = 2))
#Adding Condition
pci <- pci %>%
mutate(condition = case_when(
ranking %in% 1:2 ~ "Excellent",
ranking %in% 3:9 ~ "Good",
ranking %in% 10:41 ~ "Fair",
ranking %in% 42:61 ~ "Mediocre",
TRUE ~ "Poor"
))
# Ordering for condition
order <- c("Excellent", "Good", "Fair", "Mediocre", "Poor")
pci <- pci %>%
mutate(condition = factor(condition, levels = order, ordered = TRUE))
#Color Palette
color_palette <- viridis_pal(option = "D")(5)
#------------------------
# Data Visualization
#------------------------
pci %>%
ggplot(aes(x = cpi_index, y = reorder(province_rank, cpi_index), fill = condition)) +
geom_col() +
scale_fill_viridis(discrete = TRUE, name = "", option = "D") +
geom_text(aes(label = cpi_index_rounded), size = 3.5, hjust = -0.2) +
labs(title = "Vietnam CPI Index 2018"
, subtitle = "R Used for Data Visualization"
, caption = "Data Source: http://pci2018.pcivietnam.vn") +
theme(
axis.title.x = element_blank()
, axis.title.y = element_blank()
, axis.ticks = element_blank()
, axis.text.x = element_blank()
, panel.background = element_blank()
, axis.text.y = element_text(family = "Roboto")
, plot.title = element_text(family = "Roboto", face = "bold")
, plot.caption = element_text(family = "Roboto", hjust = 0.1, color = "grey40", face = "italic")
, plot.subtitle = element_text(family = "Roboto", color = "grey40")
) +
scale_x_continuous(expand = c(0,0), limit = c(0,80))