Load Library
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ✔ readr 2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
##
## The following object is masked from 'package:dplyr':
##
## select
library(viridis)
## Loading required package: viridisLite
Load data
# Data bản đồ thế giới
worldmap = map_data("world")
head(worldmap)
## long lat group order region subregion
## 1 -69.89912 12.45200 1 1 Aruba <NA>
## 2 -69.89571 12.42300 1 2 Aruba <NA>
## 3 -69.94219 12.43853 1 3 Aruba <NA>
## 4 -70.00415 12.50049 1 4 Aruba <NA>
## 5 -70.06612 12.54697 1 5 Aruba <NA>
## 6 -70.05088 12.59707 1 6 Aruba <NA>
# Data bản đồ 1 số nước
countries = c("Vietnam", "Laos", "Cambodia", "Thailand")
map = map_data("world", region=countries)
head(map)
## long lat group order region subregion
## 1 103.3178 10.71851 1 1 Cambodia 1
## 2 103.2812 10.67969 1 2 Cambodia 1
## 3 103.2229 10.75957 1 3 Cambodia 1
## 4 103.2234 10.78198 1 4 Cambodia 1
## 5 103.3178 10.71851 1 5 Cambodia 1
## 7 103.0451 11.28506 2 7 Cambodia Koh Kong
# Data bản đồ Việt Nam
# Get geo-spatial data for Vietnam provided:
link1 <- "https://data.opendevelopmentmekong.net/dataset/55bdad36-c476-4be9-a52d-aa839534200a/resource/b8f60493-7564-4707-aa72-a0172ba795d8/download/vn_iso_province.geojson"
vn_spatial <- sf::st_read(link1)
## Reading layer `thunhapbinhquan' from data source
## `https://data.opendevelopmentmekong.net/dataset/55bdad36-c476-4be9-a52d-aa839534200a/resource/b8f60493-7564-4707-aa72-a0172ba795d8/download/vn_iso_province.geojson'
## using driver `GeoJSON'
## Simple feature collection with 538 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 102.1421 ymin: 6.953306 xmax: 116.9473 ymax: 23.3939
## Geodetic CRS: WGS 84
head(vn_spatial)
## Simple feature collection with 6 features and 3 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 104.7765 ymin: 9.020833 xmax: 107.0322 ymax: 22.74172
## Geodetic CRS: WGS 84
## ISO3166_2_CODE Name_EN Name_VI geometry
## 1 VN-89 An Giang An Giang MULTIPOLYGON (((105.1152 10...
## 2 VN-24 Bac Giang Bắc Giang MULTIPOLYGON (((106.1654 21...
## 3 VN-6 Bac Kan Bắc Kạn MULTIPOLYGON (((105.7442 22...
## 4 VN-95 Bac Lieu Bạc Liêu MULTIPOLYGON (((105.3259 9....
## 5 VN-27 Bac Ninh Bắc Ninh MULTIPOLYGON (((106.0325 21...
## 6 VN-83 Ben Tre Bến Tre MULTIPOLYGON (((106.4251 10...
library(readxl)
vn_population <- read_excel("~/Rstudio_File/vn_population.xlsx",
col_types = c("text", "numeric", "numeric"))
head(vn_population)
## # A tibble: 6 × 3
## `Thanh Pho` `Average population` `Population density`
## <chr> <dbl> <dbl>
## 1 Ha Noi 8587. 2556.
## 2 Vinh Phuc 1211. 980.
## 3 Bac Ninh 1517. 1844.
## 4 Quang Ninh 1381. 222.
## 5 Hai Duong 1957. 1173
## 6 Hai Phong 2105. 1379.
Plots
# Vẽ bản đồ thế giới
ggplot(data=worldmap, aes(x=long, y=lat, group=group, fill=factor(group)))+
geom_polygon(col="white")+ theme(legend.position = "none")+
labs(x= "Vĩ độ", y = "Kinh độ", title= "Bản đồ thế giới")

# Vẽ bản đồ 1 số nước
ggplot(data=map, aes(x=long, y=lat, group=group))+
geom_polygon(col="white", aes(fill=region), show.legend = T)+
labs(x= "Vĩ độ", y ="Kinh độ", title= "Bản đồ khu vực")

# Vẽ bản đồ Việt Nam theo tên Tỉnh
# Vẽ bằng geom_sf vì geom_polygon cần toạ độ x, y
ggplot(vn_spatial)+
geom_sf(col="black", aes(fill=Name_EN), show.legend = FALSE)+
labs(x= "Vĩ độ", y ="Kinh độ", title= "Bản đồ Việt Nam")

# Vẽ bản đồ Việt Nam theo dân số
# Nối 2 bảng dữ liệu
vn_spatial_join <- vn_spatial %>%
full_join(vn_population, c("Name_EN" = "Thanh Pho"))
head(vn_spatial_join)
## Simple feature collection with 6 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: 104.7765 ymin: 9.020833 xmax: 107.0322 ymax: 22.74172
## Geodetic CRS: WGS 84
## ISO3166_2_CODE Name_EN Name_VI Average population Population density
## 1 VN-89 An Giang An Giang 1906.27 538.98
## 2 VN-24 Bac Giang Bắc Giang 1922.74 493.53
## 3 VN-6 Bac Kan Bắc Kạn 326.50 67.18
## 4 VN-95 Bac Lieu Bạc Liêu 925.17 346.78
## 5 VN-27 Bac Ninh Bắc Ninh 1517.44 1844.44
## 6 VN-83 Ben Tre Bến Tre 1299.33 546.00
## geometry
## 1 MULTIPOLYGON (((105.1152 10...
## 2 MULTIPOLYGON (((106.1654 21...
## 3 MULTIPOLYGON (((105.7442 22...
## 4 MULTIPOLYGON (((105.3259 9....
## 5 MULTIPOLYGON (((106.0325 21...
## 6 MULTIPOLYGON (((106.4251 10...
ggplot(vn_spatial_join)+
geom_sf(col="black", aes(fill= `Average population`))+
labs(title= " Bản đồ Việt Nam", caption = "Data Source: https://www.citypopulation.de/Vietnam.html")+
theme_void()+
scale_fill_viridis(direction = -1,
option = "C",
name = "Dân số:",
guide = guide_colourbar(direction = "horizontal",
barheight = unit(3, units = "mm"),
barwidth = unit(40, units = "mm"),
title.hjust = 0.5,
label.hjust = 0.5,
limits = c(0, 800),
title.position = "top"))+
theme(legend.position = c(0.75, 0.9))+
theme(plot.title = element_text(size = 14))+
theme(legend.title = element_text(size = 10),
legend.text = element_text(size = 8))+
annotate(label = "Hoàng Sa\n(Đà Nẵng)", "text", x = 112, y = 18, size = 3, fontface = "italic") +
annotate(label = "Trường Sa\n(Khánh Hoà)","text", x = 115.5, y = 12.5, size = 3, fontface = "italic")+
theme(panel.background = element_rect(fill = "azure", colour = "azure")) +
theme(plot.background = element_rect(fill = "azure", colour = "azure")) +
theme(legend.background = element_rect(fill = "azure", colour = "azure"))
## Warning: A numeric `legend.position` argument in `theme()` was deprecated in ggplot2
## 3.5.0.
## ℹ Please use the `legend.position.inside` argument of `theme()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
