-
Notifications
You must be signed in to change notification settings - Fork 0
/
china_map.R
72 lines (64 loc) · 3.13 KB
/
china_map.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
library(readr)
library(rworldmap)
library(maps)
library(mapdata)
library(ggplot2)
library(maps)
library(usmap)
library(data.table)
library(ggsn) # for scale bar `scalebar`
library(ggrepel)
#map("china", col = "gray40", ylim = c(18,54))
sample_dir <- "./Hackathon_2020/"
setwd(sample_dir)
library(maptools)
local_fir_dir <- "./Hackathon_2020/map/" # local the .shp file is stored
china_map <- rgdal::readOGR(paste0(local_fir_dir, "bou2_4p.shp"))
# extract province information from shap file
china_province = setDT(china_map@data)
setnames(china_province, "NAME", "province")
# transform to UTF-8 coding format
china_province[, province:=iconv(province, from = "GBK", to = "UTF-8")]
# create id to join province back to lat and long, id = 0 ~ 924
china_province[, id:= .I-1]
# there are more shapes for one province due to small islands
china_province[, table(province)]
china_province[, province:= as.factor(province)]
dt_china = setDT(fortify(china_map))
dt_china[, id:= as.numeric(id)]
setkey(china_province, id); setkey(dt_china, id)
dt_china <- china_province[dt_china]
province_CH <- china_province[, levels(province)] # the CH are in UTF-8 code
province_EN <- c("Shanghai", "Yunnan", "Inner Mongolia", "Beijing", "Taiwan",
"Jilin", "Sichuan", "Tianjin City", "Ningxia", "Anhui",
"Shandong", "Shanxi", "Guangdong", "Guangxi ", "Xinjiang",
"Jiangsu", "Jiangxi", "Hebei", "Henan", "Zhejiang",
"Hainan", "Hubei", "Hunan", "Gansu", "Fujian",
"Tibet", "Guizhou", "Liaoning", "Chongqing", "Shaanxi",
"Qinghai", "Hong Kong", "Heilongjiang")
value <- c(8893483, 12695396, 8470472, 7355291, 23193638, 9162183, 26383458, 3963604, 1945064, 19322432, 30794664, 10654162, 32222752, 13467663, 6902850, 25635291, 11847841, 20813492, 26404973, 20060115, 2451819, 17253385, 19029894, 7113833, 11971873, 689521, 10745630, 15334912, 10272559, 11084516, 1586635, 7026400, 13192935)
input_data <- data.table(province_CH, province_EN, value)
setkey(input_data, province_CH)
setkey(dt_china, province)
# remove small islands on the South China Sea
china_map_pop <- input_data[dt_china[AREA>0.1,]]
# create label file of province `label_dt`
label_dt <- china_map_pop[, .(x = mean(range(long)), y = mean(range(lat)), province_EN, province_CH), by = id]
label_dt <- unique(label_dt)
setkey(label_dt, province_EN)
# I have fine-tuned the label position of some provinces
label_dt['Inner Mongolia', `:=` (x = 110, y = 42)]
label_dt['Gansu', `:=` (x = 96.3, y = 40)]
label_dt['Hebei', `:=` (x = 115.5, y = 38.5)]
label_dt['Liaoning', `:=` (x = 123, y = 41.5)]
ggplot(china_map_pop, aes(x = long, y = lat, group = group, fill=value)) +
labs(fill = "Population (outdated)")+
geom_polygon()+
geom_path()+
scale_fill_gradientn(colours=rev(heat.colors(10)),na.value="grey90",
guide = guide_colourbar(barwidth = 0.8, barheight = 10)) +
blank() +
geom_text(data = label_dt, aes(x=x, y=y, label = province_EN),inherit.aes = F) +
scalebar(data = china_map_pop, dist = 500, dist_unit = "km",
transform = T, model = "WGS84",
border.size = 0.4, st.size = 2)