Library Loading
# Clear workspace:
rm(list = ls())
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.6 ✓ dplyr 1.0.8
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.1.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
Get Geo-spatial data
# Get geo-spatial data by district level for all provinces:
vietnam_dis <- raster::getData("GADM", country = "Vietnam", level = 2) #'GADM' is a database of global administrative boundaries, ?getData
# Only select Hanoi
hanoi <- vietnam_dis[vietnam_dis$NAME_1 == "HÃ Ná»™i", ]
Convert SpatialPolygonsDataFrame to Dataframe
# Convert to data frame
# fortify-spatial: SpatialPolygonsDataFrame to convert into a dataframe
# To figure out the correct variable name for region, inspect as.data.frame(model)
hanoi_df <- hanoi %>%
fortify(region = "NAME_2") %>% #name of variable used to split up regions
mutate(id = stri_trans_general(id, "Latin-ASCII")) # ?stri_trans_general
Prepare data for mapping
# Select some districts:
some_districts <- c("Hoan Kiem", "Hai Ba Trung", "Ba Dinh", "Tay Ho",
"Hoang Mai", "Thanh Xuan", "Nam Tu Liem", "Bac Tu Liem",
"Cau Giay", "Dong Da", "Long Bien")
# Recode label
hanoi_df %>% mutate(id = case_when(!id %in% some_districts ~ "Others", TRUE ~ id)) -> hanoi_df_relabeled
Mapping
## Loading required package: sysfonts
## Loading required package: showtextdb
font_add_google(name = "Roboto Condensed", family = "roboto") # Font selected for graph.
my_font <- "roboto"
showtext_auto()
Map 1 (Only for inner districts)
ggplot() +
geom_polygon(data = hanoi_df_relabeled %>% filter(id != "Others"),
aes(fill = id, x = long, y = lat, group = group), color = "gray69") +
scale_fill_brewer(palette = "Paired", "District:") +
theme(text = element_text(family = my_font)) +
theme(plot.margin = unit(rep(0.5, 4), "cm")) +
labs(title = "Hanoi Map, Only Inner Districts",
x = "Longitude", y = "Latitude",
caption = "Data Source: http://diva-gis.org/gdata")

Map 2 (for all districts)
ggplot() +
geom_path(data = hanoi_df_relabeled %>% filter(id == "Others"),
aes(x = long, y = lat, group = group), color = "gray69") +
geom_polygon(data = hanoi_df_relabeled %>% filter(id != "Others"),
aes(fill = id, x = long, y = lat, group = group), color = "gray69") +
scale_fill_brewer(palette = "Paired") +
theme(text = element_text(family = my_font)) +
theme(plot.margin = unit(rep(0.5, 4), "cm")) +
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.title = element_blank()) +
labs(title = "Hanoi Map by District Level",
caption = "Data Source: http://diva-gis.org/gdata")
