Practice R
Practice 3: Choropleth Maps
# Clear workspace:
rm(list = ls())
# Load data:
library(tidyverse)
library(viridis)
link <- "https://data.opendevelopmentmekong.net/dataset/999c96d8-fae0-4b82-9a2b-e481f6f50e12/resource/2818c2c5-e9c3-440b-a9b8-3029d7298065/download/diaphantinhenglish.geojson?fbclid=IwAR1coUVLkuEoJRsgaH81q6ocz1nVeGBirqpKRBN8WWxXQIJREUL1buFi1eE"
mapvn <- sf::st_read(link)
vietnam_district_level <- raster::getData("GADM", country = "Vietnam", level = 1) %>%
fortify(region = "NAME_1")
ggplot() +
geom_polygon(data = vietnam_district_level, aes(long, lat, group = group, fill = id), color = "#404040", show.legend = FALSE) +
scale_fill_viridis(discrete = TRUE, name = "") +
geom_sf(data = mapvn %>% filter(Name == "Khanh Hoa" | Name == "Da Nang"), color = "#404040", fill = NA) +
theme(panel.background = element_rect(fill = "azure"), panel.border = element_rect(fill = NA)) +
theme(plot.title = element_text(color = "grey20", size = 18, face = "bold")) +
theme(plot.caption = element_text(size = 11, colour = "grey20", face = "italic")) +
labs(x = NULL, y = NULL,
title = "Map of Vietnam",
caption = "https://data.opendevelopmentmekong.net")Trong hình tôi đánh dấu Gia Lâm vì đây là nơi tôi đang sinh sống ^^
rm(list = ls())
library(tidyverse)
library(raster)
# Lấy dữ liệu địa là của VN đến cấp xã:
df_vn <- getData("GADM", country = "Vietnam", level = 3)
hanoi <- df_vn[df_vn$NAME_1 == "HÃ Ná»™i",]
detach(package:raster)
hn <- hanoi %>% fortify(region = "NAME_3")
#plot cấp xã
hn %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = id),
color = "grey40",
show.legend = FALSE,
size = 0.01) +
labs(x = NULL, y = NULL,
title = "Map of Ha Noi Province by Commune Level") +
theme_dark()
#Cấp quáºn/huyện:
hn2 <- hanoi %>% fortify(region = "NAME_2")
hn2 %>%
ggplot(aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = id),
color = "grey40",
show.legend = FALSE,
alpha = 0.7) +
labs(x = NULL, y = NULL,
title = "Map of Ha Noi Province by District Level") +
theme_minimal()
hn_long <- hn2 %>%
group_by(id) %>%
summarise_each(funs(mean), long)
hn_lat <- hn2 %>%
group_by(id) %>%
summarise_each(funs(mean), lat)
mix <- data.frame(long = hn_long$long,
lat = hn_lat$lat,
name = hanoi$NAME_2 %>% unique())
# lấy ra tên cá»§a quáºn/huyện
mix2 <- hanoi$NAME_2 %>% unique()
#map of HN
ggplot() +
geom_polygon(data = hn2, aes(x = long, y = lat, group = group, fill = id),
color = "grey40", show.legend = FALSE, alpha = 0.7) +
geom_text(data = mix, aes(long, lat, label = mix2), size = 2.5) +
geom_point(data = mix %>% filter(name == mix2[10]), aes(long, lat), label = mix2[10], size = 2.5, color = "red") +
theme(plot.title = element_text(color = "grey20", size = 24, face = "bold")) +
labs(x = NULL, y = NULL,
title = "Map of Ha Noi Province by District Level") +
theme_minimal()