library(tidyverse)
library(sf)
library(tidyr)
library(dplyr)
library(ggplot2)
library(psych)
library(gridExtra)url <- "https://data.opendevelopmentmekong.net/dataset/55bdad36-c476-4be9-a52d-aa839534200a/resource/b8f60493-7564-4707-aa72-a0172ba795d8/download/vn_iso_province.geojson"
vn_geospatial <- st_read(url)## 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
ggplot(vn_geospatial) +
geom_sf(aes(fill = Name_EN),
show.legend = F)df <- read.csv("vndata.csv")
head(df)## code tinh_thanh dien_tich dan_so mat_do_dan_so grdp_vnd grdp_usd
## 1 VN-1 Ha Noi 3360 8436 2511 142 6093
## 2 VN-26 Vinh Phuc 1236 1198 969 128 5494
## 3 VN-27 Bac Ninh 823 1488 1809 164 7450
## 4 VN-22 Quang Ninh 6208 1363 220 199 8490
## 5 VN-30 Hai Duong 1668 1947 1167 87 3734
## 6 VN-31 Hai Phong 1527 2088 1368 174 7517
Full Join 2 tệp dữ liệu
vn_geospatial <- vn_geospatial %>%
full_join(df, c("ISO3166_2_CODE" = "code")) ## Warning in sf_column %in% names(g): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 48 of `x` matches multiple rows in `y`.
## ℹ Row 32 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
## "many-to-many"` to silence this warning.
head(vn_geospatial)## Simple feature collection with 6 features and 9 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 tinh_thanh dien_tich dan_so mat_do_dan_so
## 1 VN-89 An Giang An Giang An Giang 3537 1906 539
## 2 VN-24 Bac Giang Bắc Giang Bac Giang 3896 1891 485
## 3 VN-6 Bac Kan Bắc Kạn Bac Kan 4860 324 67
## 4 VN-95 Bac Lieu Bạc Liêu Bac Lieu 2668 922 346
## 5 VN-27 Bac Ninh Bắc Ninh Bac Ninh 823 1488 1809
## 6 VN-83 Ben Tre Bến Tre Ben Tre 2380 1298 545
## grdp_vnd grdp_usd geometry
## 1 53 2316 MULTIPOLYGON (((105.1152 10...
## 2 82 3542 MULTIPOLYGON (((106.1654 21...
## 3 47 1989 MULTIPOLYGON (((105.7442 22...
## 4 59 2605 MULTIPOLYGON (((105.3259 9....
## 5 164 7450 MULTIPOLYGON (((106.0325 21...
## 6 49 2106 MULTIPOLYGON (((106.4251 10...
dim(vn_geospatial)## [1] 539 10
Xem cấu trúc dữ liệu
glimpse(vn_geospatial)## Rows: 539
## Columns: 10
## $ ISO3166_2_CODE <chr> "VN-89", "VN-24", "VN-6", "VN-95", "VN-27", "VN-83", "V…
## $ Name_EN <chr> "An Giang", "Bac Giang", "Bac Kan", "Bac Lieu", "Bac Ni…
## $ Name_VI <chr> "An Giang", "Bắc Giang", "Bắc Kạn", "Bạc Liêu", "Bắc Ni…
## $ tinh_thanh <chr> "An Giang", "Bac Giang", "Bac Kan", "Bac Lieu", "Bac Ni…
## $ dien_tich <int> 3537, 3896, 4860, 2668, 823, 2380, 6066, 2695, 6874, 79…
## $ dan_so <int> 1906, 1891, 324, 922, 1488, 1298, 1504, 2763, 1035, 125…
## $ mat_do_dan_so <int> 539, 485, 67, 346, 1809, 545, 248, 1025, 151, 158, 229,…
## $ grdp_vnd <int> 53, 82, 47, 59, 164, 49, 71, 165, 85, 78, 62, 86, 40, 5…
## $ grdp_usd <int> 2316, 3542, 1989, 2605, 7450, 2106, 3038, 7339, 3657, 3…
## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((105.1152 10..., MULTIPOLYG…
Kiểm tra NA
colSums(is.na(vn_geospatial))## ISO3166_2_CODE Name_EN Name_VI tinh_thanh dien_tich
## 0 0 0 0 0
## dan_so mat_do_dan_so grdp_vnd grdp_usd geometry
## 0 0 0 0 0
attach(vn_geospatial)
ggplot(vn_geospatial) +
geom_sf(aes(fill = grdp_vnd),
size = 0.4) +
theme_void() +
labs(
title = "GRDP by Province in Vietnam 2022",
caption = "Data Source: GSO",
fill = "GRDP (VNĐ)"
) +
scale_fill_continuous(
breaks = c(100,200,300),
labels = c('100 M','200 M','300 M'),
) +
theme(
plot.background = element_rect(fill = "azure", colour = "azure"),
panel.background = element_rect(fill = "azure", colour = "azure"),
legend.background = element_rect(fill = "azure", colour = "azure"),
plot.title = element_text(face = "bold")
) +
annotate(
label = "Hoang Sa\n(Da Nang)",
"text",
x = 112,
y = 18,
size = 3,
fontface = "italic"
) +
annotate(
label = "Truong Sa\n(Khanh Hoa)",
"text",
x = 115.5,
y = 12.5,
size = 3,
fontface = "italic"
)