# Clear R environment:
rm(list = ls())
# Setwd
setwd("D:/0 - My documents/TOOLS/R/The Economist/Vietnam Map")
# Reference: http://pci2018.pcivietnam.vn/uploads/2019/ho-so-63-tinh-vie.pdf
# Data Source: http://pci2018.pcivietnam.vn/
#cpi_colors <- c("#892890", "#034EA2", "#4792CF", "#8ED8F8", "#BAE1D1")
# Pacman: Load necessary packages
library(pacman)
pacman::p_load(
viridis,
tidyverse,
rio,
extrafont,
showtext
)
# Import data
df_cpi <- import("D:/0 - My documents/TOOLS/R/The Economist/Vietnam Map/PCI/Data/PCI_2018.xlsx")%>%
select(1:3) %>%
slice(1:63)
names(df_cpi) <- c("Province", "Rank", "Score")
df_ploting <- df_cpi %>%
mutate(Province = case_when(str_detect(Province, "BRVT") ~ "Bà Rịa - Vũng Tàu", TRUE ~ Province)) %>%
mutate(fake_rank = case_when(Rank < 10 ~ paste0("0", Rank), TRUE ~ as.character(Rank))) %>%
mutate(Province = paste(Province, fake_rank)) %>%
mutate(my_colors = case_when(Rank <= 2 ~ "Excellent",
Rank >= 3 & Rank <= 9 ~ "Good",
Rank >= 10 & Rank <= 41 ~ "Fair",
Rank >= 42 & Rank <= 61 ~ "Mediocre",
TRUE ~ "Poor")) %>%
arrange(-Rank) %>%
mutate(Province = factor(Province, levels = Province)) %>%
mutate(my_colors = factor(my_colors, levels = my_colors %>% unique() %>% .[5:1])) %>%
mutate(label = round(Score, 2) %>% as.character()) %>%
mutate(label = case_when(str_count(label) == 2 ~ paste0(label, ".00"),
str_count(label) == 4 ~ paste0(label, "0"),
TRUE ~ label))
#-------------
# Bar Plot
#-------------
showtext.auto()
my_font <- "Roboto Condensed"
font_add_google(name = my_font, family = my_font)
p1 <- df_ploting %>%
ggplot(aes(Province, Score, fill = my_colors)) +
geom_col(width = 0.85) +
coord_flip()+
# Apply the viridis color palette in R
scale_fill_viridis(discrete = TRUE, name = "", option = "D")+
geom_text(aes(label = label), size = 3, hjust = -0.1)+
scale_y_continuous(limits = c(0, 80), expand = c(0.001, 0))+
theme_minimal()+
theme(panel.grid = element_blank()) +
theme(axis.text.x = element_blank())+
theme(axis.text.y = element_text(size = 8, family = my_font, color = "black"))+
theme(plot.title = element_text(family = my_font, color = "grey20", size = 22, face = "bold"))+
theme(plot.subtitle = element_text(family = my_font, size = 10, color = "gray40")) +
theme(plot.caption = element_text(family = my_font, size = 11, colour = "grey40", face = "italic")) +
theme(legend.text = element_text(family = my_font, size = 10)) +
theme(plot.margin = unit(c(1, 1, 0.5, 1), "cm")) +
labs(x = NULL, y = NULL,
title = "Vietnam CPI Index 2018",
subtitle = "R Used for Data Visualization",
caption = "Data Source: http://pci2018.pcivietnam.vn")
print(p1)#--------------
# Mapping
#--------------
# Get geospatial data for Viet Nam:
library(raster)
vietnam_province <- getData("GADM", country = "Vietnam", level = 1)
detach(package:raster)
vietnam_df <- vietnam_province %>% fortify(region = "NAME_1")
library(stringi)
vietnam_df <- vietnam_df %>%
mutate(id_prov = stri_trans_general(id, "Latin-ASCII")) %>%
mutate(id_prov = case_when(str_detect(id_prov, "Ba Ria") ~ "BRVT",
str_detect(id_prov, "Ho Chi Minh") ~ "TP.HCM",
str_detect(id_prov, "Thua Thien Hue") ~ "TT-Hue",
TRUE ~ id_prov))
df_cpi <- df_cpi %>%
mutate(id_prov = stri_trans_general(Province, "Latin-ASCII"))
# Joint data sets:
df_cpi_mapping <- right_join(vietnam_df, df_cpi, by = "id_prov")
p2 <- ggplot() +
geom_polygon(data = df_cpi_mapping, aes(long, lat, group = group, fill = Score), color = "white") +
coord_map("albers", lat0 = 30, lat1 = 40)+
labs(title = "Vietnam CPI Index 2018",
subtitle = "Vietnam's Paracel and Spratly Islands\nare not shown in this map.",
caption = "Data Source: http://pci2018.pcivietnam.vn")+
theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid = element_blank(),
plot.background = element_rect(fill = "white", color = NA),
panel.background = element_rect(fill = "white", color = NA),
legend.background = element_rect(fill = "white", color = NA),
panel.border = element_blank())+
theme(plot.title = element_text(family = my_font, color = "grey20", size = 22, face = "bold")) +
theme(plot.subtitle = element_text(family = my_font, size = 10, color = "gray40")) +
theme(plot.caption = element_text(family = my_font, size = 11, colour = "grey40", face = "italic")) +
theme(legend.text = element_text(family = my_font, color = "grey40", size = 10)) +
theme(legend.title = element_text(family = my_font, color = "grey20", size = 10)) +
theme(plot.margin = unit(c(1, 1, 0.5, 1), "cm"))+
theme(legend.position = c(0.3, 0.5)) +
scale_fill_viridis(direction = -1,
option = "D",
name = "CPI Index",
guide = guide_colourbar(direction = "horizontal",
barheight = unit(3, units = "mm"),
barwidth = unit(40, units = "mm"),
title.hjust = 0.5,
label.hjust = 0.5,
title.position = "top"))
print(p2)
gridExtra::grid.arrange(p2, p1, ncol = 2)# Clear R environment:
rm(list = ls())
# Setwd
setwd("D:/0 - My documents/TOOLS/R/The Economist/Vietnam Map")
# Reference: https://pcivietnam.vn/uploads//VN-Bao-cao-dai-PCI/Bao-cao-PCI-2022.pdf
# Data Source: http://pci2022.pcivietnam.vn/
# Pacman: Load necessary packages
library(pacman)
pacman::p_load(
viridis,
tidyverse,
rio,
extrafont,
showtext
)
# Import data
df_cpi21 <- import("D:/0 - My documents/TOOLS/R/The Economist/Vietnam Map/PCI/Data/PCI_2021.xlsx")%>%
select(1:2) %>%
slice(1:63)
names(df_cpi21) <- c("Province", "Score")
df_cpi21 <- df_cpi21 %>%
mutate(Rank = rank(-Score))
df_ploting21 <- df_cpi21 %>%
mutate(Province = case_when(str_detect(Province, "BRVT") ~ "Bà Rịa - Vũng Tàu", TRUE ~ Province)) %>%
mutate(fake_rank = case_when(Rank < 10 ~ paste0("0", Rank), TRUE ~ as.character(Rank))) %>%
mutate(Province = paste(Province, fake_rank)) %>%
mutate(my_colors = case_when(Rank <= 1 ~ "Excellent",
Rank >= 2 & Rank <= 12 ~ "Good",
Rank >= 13 & Rank <= 32 ~ "Fair",
Rank >= 33 & Rank <= 54 ~ "Mediocre",
Rank >= 55 & Rank <= 61 ~ "Low",
TRUE ~ "Poor")) %>%
arrange(-Rank) %>%
mutate(Province = factor(Province, levels = Province)) %>%
mutate(my_colors = factor(my_colors, levels = my_colors %>% unique() %>% .[6:1])) %>%
mutate(label = round(Score, 2) %>% as.character()) %>%
mutate(label = case_when(str_count(label) == 2 ~ paste0(label, ".00"),
str_count(label) == 4 ~ paste0(label, "0"),
TRUE ~ label))
#-------------
# Bar Plot
#-------------
showtext.auto()
my_font <- "Roboto Condensed"
font_add_google(name = my_font, family = my_font)
p3 <- df_ploting21 %>%
ggplot(aes(Province, Score, fill = my_colors)) +
geom_col(width = 0.85) +
coord_flip()+
# Apply the viridis color palette in R
scale_fill_viridis(discrete = TRUE, name = "", option = "D")+
geom_text(aes(label = label), size = 3, hjust = -0.1)+
scale_y_continuous(limits = c(0, 85), expand = c(0.001, 0))+
theme_minimal()+
theme(panel.grid = element_blank()) +
theme(axis.text.x = element_blank())+
theme(axis.text.y = element_text(size = 8, family = my_font, color = "black"))+
theme(plot.title = element_text(family = my_font, color = "grey20", size = 22, face = "bold"))+
theme(plot.subtitle = element_text(family = my_font, size = 10, color = "gray40")) +
theme(plot.caption = element_text(family = my_font, size = 11, colour = "grey40", face = "italic")) +
theme(legend.text = element_text(family = my_font, size = 10)) +
theme(plot.margin = unit(c(1, 1, 0.5, 1), "cm")) +
labs(x = NULL, y = NULL,
title = "Vietnam CPI Index 2021",
subtitle = "R Used for Data Visualization",
caption = "Data Source: http://pci2021.pcivietnam.vn")
print(p3)#--------------
# Mapping
#--------------
# Get geospatial data for Viet Nam:
library(raster)
vietnam_province <- getData("GADM", country = "Vietnam", level = 1)
detach(package:raster)
vietnam_df <- vietnam_province %>% fortify(region = "NAME_1")
library(stringi)
vietnam_df <- vietnam_df %>%
mutate(id_prov = stri_trans_general(id, "Latin-ASCII")) %>%
mutate(id_prov = case_when(str_detect(id_prov, "Ba Ria") ~ "BRVT",
str_detect(id_prov, "Ho Chi Minh") ~ "TP.HCM",
str_detect(id_prov, "Thua Thien Hue") ~ "TT-Hue",
TRUE ~ id_prov))
df_cpi21 <- df_cpi21 %>%
mutate(id_prov = stri_trans_general(Province, "Latin-ASCII"))
# Joint data sets:
df_cpi21_mapping <- right_join(vietnam_df, df_cpi21, by = "id_prov")
p4 <- ggplot() +
geom_polygon(data = df_cpi21_mapping, aes(long, lat, group = group, fill = Score), color = "white") +
coord_map("albers", lat0 = 30, lat1 = 40)+
labs(title = "Vietnam CPI Index 2021",
subtitle = "Vietnam's Paracel and Spratly Islands\nare not shown in this map.",
caption = "Data Source: http://pci2021.pcivietnam.vn")+
theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid = element_blank(),
plot.background = element_rect(fill = "white", color = NA),
panel.background = element_rect(fill = "white", color = NA),
legend.background = element_rect(fill = "white", color = NA),
panel.border = element_blank())+
theme(plot.title = element_text(family = my_font, color = "grey20", size = 22, face = "bold")) +
theme(plot.subtitle = element_text(family = my_font, size = 10, color = "gray40")) +
theme(plot.caption = element_text(family = my_font, size = 11, colour = "grey40", face = "italic")) +
theme(legend.text = element_text(family = my_font, color = "grey40", size = 10)) +
theme(legend.title = element_text(family = my_font, color = "grey20", size = 10)) +
theme(plot.margin = unit(c(1, 1, 0.5, 1), "cm"))+
theme(legend.position = c(0.3, 0.5)) +
scale_fill_viridis(direction = -1,
option = "D",
name = "CPI Index",
guide = guide_colourbar(direction = "horizontal",
barheight = unit(3, units = "mm"),
barwidth = unit(40, units = "mm"),
title.hjust = 0.5,
label.hjust = 0.5,
title.position = "top"))
print(p4)
gridExtra::grid.arrange(p4, p3, ncol = 2)