Chuẩn bị dữ liệu địa lý
library(tidyverse)
## ── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✓ ggplot2 3.3.0 ✓ purrr 0.3.3
## ✓ tibble 2.1.3 ✓ dplyr 0.8.3
## ✓ tidyr 1.0.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Warning: package 'sf' was built under R version 3.6.2
## Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(ggplot2)
library(readxl)
library(readr)
library(tmap)
## Warning: package 'tmap' was built under R version 3.6.2
library(tmaptools)
## Warning: package 'tmaptools' was built under R version 3.6.2
library(leaflet)
library(knitr)
vn <- st_read("~/OneDrive/Teaching/R project/Map/VNM_adm/gadm36_VNM_1.shp")
## Reading layer `gadm36_VNM_1' from data source `/Users/ngocnguyen/OneDrive/Teaching/R project/Map/VNM_adm/gadm36_VNM_1.shp' using driver `ESRI Shapefile'
## Simple feature collection with 63 features and 10 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 102.1446 ymin: 8.381355 xmax: 109.4692 ymax: 23.39269
## CRS: 4326
vn %>% class
## [1] "sf" "data.frame"
Download data tại: https://github.com/ngocdlu/distribution/blob/master/gadm36_VNM_1.shp
qtm(vn)
theme_set(
theme_minimal() +
theme(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank()))
vn %>%
ggplot() +
geom_sf(aes(fill = NAME_1)) +
theme(legend.position = "none")
Tạo bản đồ nhiệt mật độ phân bố của đối tượng nghiên cứu
Trước tiên chúng ta cần chuẩn bị dữ liệu phân bố của đối tượng nghiên cứu, tổ chức dữ liệu có cấu trúc tương đồng về tỉnh thành trong đó có ít nhất 1 biến trùng tên với 1 biến trong dữ liệu địa lý ở trên (tốt nhất là biến mã ID của tỉnh (GID_1) hoặc biến tên tỉnh dạng tiếng anh (VANAME_1)).
Load dữ liệu đã chuẩn bị từ máy tính cá nhân hoạt Github
dist <- read_excel("~/OneDrive/Teaching/R project/RMarkdown/Distribution.xlsx")
Download data tại https://github.com/ngocdlu/distribution/blob/master/Distribution.csv (dataframe "dist cũng có 7 biến và 63 observation)
Dùng lệnh “View()” để xem dữ liệu VD: View(vn) hay View(dist)
vn <- merge(vn, dist, by = "GID_1")
Như vậy hiện tại ta đã có dataframe “vn” mới với 17 biến và 63 observaion (đã thêm các biên từ data dist)
vn %>%
ggplot() +
geom_sf(aes(fill = N_2015)) +
scale_fill_gradient(low="white", high="blue")
- Thêm tiêu đề bản đồ:
vn %>%
ggplot() +
geom_sf(aes(fill = N_2015)) +
scale_fill_gradient(low="white", high="blue") +
labs(title = "Mật độ phân bố loài Lithocarpus sp. tại Việt Nam 2015",
caption = "Ngoc N.V. inprep.")
Load dữ liệu địa lý
Vì xây dựng bản đổ phân bố theo huyện nên ta sẽ sử dụng dữ liệu địa lý cấp 2 của Việt Nam
vn1 <- st_read("~/OneDrive/Teaching/R project/Map/VNM_adm/gadm36_VNM_2.shp")
## Reading layer `gadm36_VNM_2' from data source `/Users/ngocnguyen/OneDrive/Teaching/R project/Map/VNM_adm/gadm36_VNM_2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 710 features and 13 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 102.1446 ymin: 8.381355 xmax: 109.4692 ymax: 23.39269
## CRS: 4326
vn1 %>% class
## [1] "sf" "data.frame"
Download data từ Github: https://github.com/ngocdlu/distribution/blob/master/gadm36_VNM_2.shp
VD: Xây dựng bản đồ phân bố loài Lithocarpus sp. tại Lâm Đồng
Trong dataframe vn1 chúng ta tìm đến tỉnh Lâm Đồng (Từ row 405 đến 416), ta sẽ thực hiện extract dữ liệu địa lý của tỉnh Lâm Đồng bằng lệnh sau:
ld <-vn1[(405:416),]
Tạo bản đồ nhiệt mật độ phân bố của đối tượng nghiên cứu theo huyện
ld1 <- read_excel("~/OneDrive/Teaching/R project/RMarkdown/ld_huyen.xlsx")
hoặc load data tại https://github.com/ngocdlu/distribution/blob/master/ld_huyen.csv
ld2 <- merge(ld, ld1, by = "GID_2")
Chúng ta đã có dataframe “ld2” với 20 biến và 12 obs.
ld2 %>%
ggplot() +
geom_sf(aes(fill = N_2015)) +
ggtitle("Mật độ phân bố loài Lithocarpus sp. tại Lâm Đồng 2015") +
scale_fill_gradient(low="white", high="blue")
ld2 %>%
ggplot() +
geom_sf(aes(fill = N_2015)) +
geom_sf_text(aes(label = VARNAME_2)) +
scale_fill_gradient(low="white", high="blue") +
labs(title = "Mật độ phân bố loài Lithocarpus sp. tại Lâm Đồng 2015",
caption = "Ngoc N.V. inprep.")
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
——-End——–
Ngọc Nguyễn