GADM (without Spratly Islands & Paracel Islands)

Prepare Data

# Clear R environment: 
rm(list = ls())

# Setwd
setwd("D:/0 - My documents/TOOLS/R/The Economist/Vietnam Map")


# Pacman: Load necessary packages
library(pacman)
pacman::p_load(
  tidyverse,
  magrittr,
  ggplot2,
  cowplot,
  raster,
  maptools,
  gpclib,
  rgeos,
  rgdal,
  rgeos,
  rgdal
)

# Get Vietnam's geography data at commune level

vietnam_full <- getData("GADM", country = "Vietnam", level = 3)
#1 - Province, 2 - District, 3 - Commune

# Data structure: 
vietnam_full %>% class()

Province Level

# Province Map
{
    # transforming to regular form: 
    province_df <- vietnam_full %>% fortify(region = "NAME_1")
    # Province Vietnam data
    province <- province_df$id %>% unique()
    province
    # Map Province Vietnam
    ggplot() + 
      geom_polygon(data = province_df, aes(long, lat, group = group, fill = id), 
                   show.legend = FALSE)+
      # Add boundary
      geom_path(data = province_df, aes(long, lat, group = group), 
                color = "white", size = 0.1)+
      # Keep on adjusting
      coord_fixed(1)+
      coord_equal()+
      coord_map("albers", lat0 = 30, lat1 = 40)+
      coord_map("gilbert")
      # Map to other df
    {
    # Simulating a sale dataset
    set.seed(0908487291)
    province_sale <- data.frame(id = province, 
                       sale = rnorm(province %>% length(), 30, 3000) %>% abs %>% round(0))
      
    # Joint two datasets: 
    province_sale_map <- right_join(province_df, province_sale, by = "id")
    
    # Mapping
    ggplot() + 
      geom_polygon(data = province_sale_map, aes(long, lat, group = group, fill = sale))+
      theme_map() +
      # Add boundary
      geom_path(data = province_sale_map, aes(long, lat, group = group), 
                color = "white", size = 0.1)+
      # Adjust background
      theme(plot.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
      theme(panel.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
      # Adjust title, sub, caption
      labs(x = NULL, y = NULL, 
           title = "Vienam Map - Province Level", 
           subtitle = "Sale distribution (million VND)", 
           caption = "Source: Dept. of Business",
           fill = "Sale")+
      theme(plot.title = element_text(size = 15, color = "grey30", face = "bold"))+
      theme(plot.subtitle = element_text(size = 10, color = "grey50", face = "italic"))+
      theme(plot.caption = element_text(size = 5, color = "grey50", face = "italic")) +
      # Adjust legend
      theme(legend.title = element_text(size = 8, color = "grey50"))+
      theme(legend.text = element_text(size = 5, color = "grey50"))+
      theme(legend.key.height = unit(0.3, units = "cm"))+
      theme(legend.key.width = unit(0.2, units = "cm"))
    
    ggsave("province.png", width = 3, height = 5,dpi = 300,units = c("in"))
      }
}

District Level

# District Map
{
    # extract data
    province_full <- vietnam_full[vietnam_full$NAME_1 == "Hồ Chí Minh", ]
    province_full
    # transforming data : 
    district_df <- province_full %>% fortify(region = "NAME_2")
    district_df
    # List District in Province
    district <- district_df$id %>% unique()
    district
    # Map District Vietnam
    ggplot() + 
      geom_polygon(data = district_df, aes(long, lat, group = group, fill = id), 
                   show.legend = FALSE)+
      # Add boundary
      geom_path(data = district_df, aes(long, lat, group = group), 
                color = "white", size = 0.1)+
      # Keep on adjusting
      coord_fixed(1)+
      coord_equal()+
      coord_map("albers", lat0 = 30, lat1 = 40)+
      coord_map("gilbert")
    # Map to other df
    {
      # Simulating a sale dataset
      set.seed(0908487291)
      district_sale <- data.frame(id = district, 
                                 sale = rnorm(district %>% length(), 30, 3000) %>% abs %>% round(0))
      
      # Joint two datasets: 
      district_sale_map <- right_join(district_df, district_sale, by = "id")
      
      # Mapping
      ggplot() + 
        geom_polygon(data = district_sale_map, aes(long, lat, group = group, fill = sale))+
        theme_map() +
        # Add boundary
        geom_path(data = district_sale_map, aes(long, lat, group = group), 
                  color = "white", size = 0.1)+
        # Adjust background
        theme(plot.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
        theme(panel.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
        # Adjust title, sub, caption
        labs(x = NULL, y = NULL, 
             title = "Vienam Map - District Level", 
             subtitle = "Sale distribution (million VND)", 
             caption = "Source: Dept. of Business",
             fill = "Sale")+
        theme(plot.title = element_text(size = 15, color = "grey30", face = "bold"))+
        theme(plot.subtitle = element_text(size = 10, color = "grey50", face = "italic"))+
        theme(plot.caption = element_text(size = 5, color = "grey50", face = "italic")) +
        # Adjust legend
        theme(legend.title = element_text(size = 8, color = "grey50"))+
        theme(legend.text = element_text(size = 5, color = "grey50"))+
        theme(legend.key.height = unit(0.3, units = "cm"))+
        theme(legend.key.width = unit(0.2, units = "cm"))
      
      ggsave("district.png", width = 3, height = 3,dpi = 300,units = c("in"))
      }
}  

Commune Level

# Commune Map
{
  # extract data
  district_full <- vietnam_full[vietnam_full$NAME_2 == "Phú Nhuận", ]
  district_full
  # transforming data : 
  commune_df <- district_full %>% fortify(region = "NAME_3")
  commune_df
  # List Commune in Province
  commune <- commune_df$id %>% unique()
  commune
  # Map Commune Vietnam
  ggplot() + 
    geom_polygon(data = commune_df, aes(long, lat, group = group, fill = id), 
                 show.legend = FALSE)+
    # Add boundary
    geom_path(data = commune_df, aes(long, lat, group = group), 
              color = "white", size = 0.1)+
    # Keep on adjusting
    coord_fixed(1)+
    coord_equal()+
    coord_map("albers", lat0 = 30, lat1 = 40)+
    coord_map("gilbert")
  # Map to other df
  {
    # Simulating a sale dataset
    set.seed(0908487291)
    commune_sale <- data.frame(id = commune, 
                                sale = rnorm(commune %>% length(), 30, 3000) %>% abs %>% round(0))
    
    # Joint two datasets: 
    commune_sale_map <- right_join(commune_df, commune_sale, by = "id")
    
    # Mapping
    ggplot() + 
      geom_polygon(data = commune_sale_map, aes(long, lat, group = group, fill = sale))+
      theme_map() +
      # Add boundary
      geom_path(data = commune_sale_map, aes(long, lat, group = group), 
                color = "white", size = 0.1)+
      # Adjust background
      theme(plot.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
      theme(panel.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
      # Adjust title, sub, caption
      labs(x = NULL, y = NULL, 
           title = "Vienam Map - Commune Level", 
           subtitle = "Sale distribution (million VND)", 
           caption = "Source: Dept. of Business",
           fill = "Sale")+
      theme(plot.title = element_text(size = 15, color = "grey30", face = "bold"))+
      theme(plot.subtitle = element_text(size = 10, color = "grey50", face = "italic"))+
      theme(plot.caption = element_text(size = 5, color = "grey50", face = "italic")) +
      # Adjust legend
      theme(legend.title = element_text(size = 8, color = "grey50"))+
      theme(legend.text = element_text(size = 5, color = "grey50"))+
      theme(legend.key.height = unit(0.3, units = "cm"))+
      theme(legend.key.width = unit(0.2, units = "cm"))
    
    ggsave("commune.png", width = 4, height = 3,dpi = 300,units = c("in"))
    }
  
}

OpenDevelopment Mekong

Vietnam

# Clear workspace: 
rm(list = ls())

# Load tidyverse package: 

library(tidyverse)

# Vietnam Map (include Spratly Islands and Paracel Islands) : 

link <- "https://data.opendevelopmentmekong.net/dataset/999c96d8-fae0-4b82-9a2b-e481f6f50e12/resource/2818c2c5-e9c3-440b-a9b8-3029d7298065/download/diaphantinhenglish.geojson?fbclid=IwAR1coUVLkuEoJRsgaH81q6ocz1nVeGBirqpKRBN8WWxXQIJREUL1buFi1eE"

vn_spatial <- sf::st_read(link)

v1 <- ggplot() + 
  geom_sf(data = vn_spatial) + 
  labs(title = "Vietnam Map - Version 1")

v2 <-ggplot() + 
  geom_sf(data = vn_spatial, aes(fill = Name), show.legend = FALSE) + 
  labs(title = "Vietnam Map - Version 2")

gridExtra::grid.arrange(v1, v2, ncol = 2)

Region & Sub-region

# Region and Sub-region

# Scrap list of provinces by region and sub-region level: 

library(rvest)

# Collect region/sub-region data from Wiki: 

provinces <- "https://en.wikipedia.org/wiki/Provinces_of_Vietnam"

provinces_vn <- provinces %>% 
  read_html() %>% 
  html_nodes(xpath = '//*[@id="mw-content-text"]/div[1]/table[6]') %>% 
  html_table() %>% 
  .[[1]]

names(provinces_vn)

library(stringi)

provinces_vn <- provinces_vn %>% 
  select("Province/city", "Region")

names(provinces_vn) <- c("Province", "Region")

province_region_vietnam <- provinces_vn %>% 
  mutate(prov_eng = stri_trans_general(Province, "Latin-ASCII")) %>% 
  mutate(prov_eng = str_replace_all(prov_eng, " City", "")) %>% 
  mutate(prov_eng = str_replace_all(prov_eng, " city", "")) %>% 
  mutate(prov_eng = str_replace_all(prov_eng, " province", "")) %>%
  mutate(prov_eng = case_when(prov_eng == "Thua Thien-Hue" ~ "Thua Thien - Hue", 
                              prov_eng == "Ba Ria-Vung Tau" ~ "Ba Ria - Vung Tau", 
                              prov_eng == "Ho Chi Minh" ~ "TP. Ho Chi Minh", 
                                    TRUE ~ prov_eng))

# Combine the two datasets: 

full_join(vn_spatial, province_region_vietnam, by = c("Name" = "prov_eng")) -> vn_spatial_region

# Administrative map of Vietnam: 

ggplot() + 
  geom_sf(data = vn_spatial_region, aes(fill = Region)) + 
  labs(title = "Vietnam Map - Region Level")+
  theme(plot.title = element_text(size = 16, face = "bold"))

Join to other data frame

# List Region in Vietnam
region <- vn_spatial_region$Region %>% unique()
region
  
# Map to other df
{
  # Simulating a sale dataset
  set.seed(0908487291)
  region_sale <- data.frame(id = region, 
                              sale = rnorm(region %>% length(), 30, 3000) %>% abs %>% round(0))
  
  # Joint two datasets: 
  region_sale_map <- left_join(vn_spatial_region, region_sale, by = c("Region" = "id"))
  
  # Mapping
  ggplot() + 
    geom_sf(data = region_sale_map, aes(fill = sale), color = "grey30", size = 0.1)+
    theme_map()+
    # Adjust background
    theme(plot.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
    theme(panel.background = element_rect(fill = "#f0f0f0", color = "#f0f0f0")) +
    # Adjust title, sub, caption
    labs(x = NULL, y = NULL, 
         title = "Vienam Map - Region Level", 
         subtitle = "Sale distribution (million VND)", 
         caption = "Source: Dept. of Business",
         fill = "Sale")+
    theme(plot.title = element_text(size = 50, color = "grey30", face = "bold"))+
    theme(plot.subtitle = element_text(size = 30, color = "grey50", face = "italic"))+
    theme(plot.caption = element_text(size = 30, color = "grey50", face = "italic")) +
    # Adjust legend
    theme(legend.title = element_text(size = 30, color = "grey50"))+
    theme(legend.text = element_text(size = 20, color = "grey50"))+
    theme(legend.key.height = unit(0.3, units = "cm"))+
    theme(legend.key.width = unit(0.2, units = "cm"))
  
  ggsave("region.png", width = 5, height = 5,dpi = 300,units = c("in"))
  
  }