Motivations

Dưới đây là một Barplot với cờ tương ứng của quốc gia:

R Codes

Dưới đây là R codes để tạo barplot ở trên:

# Clear R environment: 

rm(list = ls())


#----------------------------
# Extract openess indicators
#----------------------------

library(rvest) # -> For scraping data from web. 
library(dplyr) # -> For data manipulation. 

"https://www.theglobaleconomy.com/rankings/trade_openness/" %>% read_html() -> html_content

html_content %>% 
  html_nodes(".graph_outside_link") %>% 
  html_text() -> countries

html_content %>% 
  html_nodes(".fl .fl .fl div div div") %>% 
  html_text() %>% 
  as.numeric() -> openess

openess_data <- data.frame(country = countries, openess = openess)

# Select top-25 countries: 

openess_data %>% top_n(n = 25, wt = openess) -> openess_top25

#-----------------------
# Extract country codes
#-----------------------

"https://www.iban.com/country-codes" %>% 
  read_html() %>% 
  html_nodes(xpath = '//*[@id="myTable"]') %>% 
  html_table() %>% 
  .[[1]] -> country_code_data

#---------------------------
# Prepare data for ploting
#---------------------------

# Select two column and rename: 

country_code_data %>% select(country = Country, code = `Alpha-2 code`) -> df_code

# Merge the two data frame: 

full_join(openess_top25, df_code, by = "country") %>% filter(!is.na(openess)) -> openess_top25_updated

# Fill country codes for some missing cases: 

library(stringr) # -> For text processing. 

openess_top25_updated %>% 
  mutate(code = case_when(str_detect(country, "iet") ~ "VN", 
                          str_detect(country, "mirate") ~ "AE", 
                          str_detect(country, "therlan") ~ "NL", 
                          str_detect(country, "arb") ~ "BB", 
                          TRUE ~ code)) %>% 
  mutate(country = case_when(str_detect(country, "Ant.") ~ "Barbados", 
                             TRUE ~ country)) -> openess_top25_updated_again


openess_top25_updated_again %>% 
  arrange(openess) %>% 
  mutate(country_fct = factor(country, levels = country)) -> data_for_ploting

#----------------------------
#  Make a draft of bar plot
#----------------------------

library(ggplot2)
library(ggimage)

my_colors <- c("#014d64", "#01a2d9") # -> Color for bar. 

data_for_ploting %>% 
  ggplot(aes(y = country_fct, x = openess)) + 
  geom_col(width = 0.85, fill = my_colors[2]) + 
  geom_flag(x = -13, aes(image = code)) -> bar_draft

#-------------------------------
#  Make the draft more elegant
#-------------------------------

library(showtext) # -> Package for using extra fonts. 

my_font <- "Roboto Condensed" # -> Set Roboto Condensed font for bar plot: 

font_add_google(name = my_font, family = my_font) # -> # Load font for ploting. 

showtext_auto() # -> # Automatically use showtext to render text. 

p_title <- "Trade openness by exports plus imports as percent of GDP, 2020"

p_subtitle <- "The average for 2020 based on 157 countries was 82.68 percent. The highest value was\nin Luxembourg: 376.3 percent and the lowest value was in Sudan: 0.78 percent."

p_caption <- "Data Source: https://www.theglobaleconomy.com"

bar_draft + 
  theme_minimal() + 
  scale_x_continuous(limits = c(-22, 400), expand = c(0, 0), breaks = seq(0, 400, 50), position = "top") + 
  labs(title = p_title, subtitle = p_subtitle, caption = p_caption) + 
  theme(axis.title = element_blank()) + 
  theme(plot.margin = unit(c(1, 1.3, 1, 1), "cm")) + 
  theme(plot.background = element_rect(fill = "seashell", color = NA)) + 
  theme(panel.grid.minor = element_blank()) + 
  theme(panel.grid.major.y = element_blank()) + 
  theme(panel.grid.major.x = element_line(size = 0.7, color = "grey80")) + 
  theme(plot.title = element_text(family = my_font, size = 20, color = "black")) + 
  theme(plot.subtitle = element_text(family = my_font, size = 11, color = "grey10")) + 
  theme(plot.caption = element_text(family = my_font, size = 9, face = "italic", vjust = -4)) + 
  theme(axis.text = element_text(family = my_font, size = 11))