Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

xmc811/mapchina

Repository files navigation

mapchina

An R package storing the geospatial shapefile (vector data) of China administrative divisions to the county/district-level.

Zhong Hua Ren Min Gong He Guo Qu Xian Ji Xing Zheng Qu Hua Shi Liang Di Tu Shu Ju

Installation

To install CRAN version An Zhuang CRANZheng Shi Ban Ben :

install.packages("mapchina")

To install the most updated version of the 'dev' branch An Zhuang Kai Fa Zhong De Zui Xin Ban Ben :

if (!require(devtools)) {
install.packages("devtools")
}

devtools::install_github("xmc811/mapchina", ref = "dev")

Examples Shi Yong Shi Li

1. Browsing the dataframe of the shapefile

Cha Kan Shi Liang Di Tu Shu Ju

library(mapchina)
head(china)
# Output Shu Chu
Simple feature collection with 6 features and 13 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 115.4248 ymin: 39.44473 xmax: 116.8805 ymax: 41.05936
geographic CRS: WGS 84
# A tibble: 6 x 14
Code_County Code_Perfecture Code_Province Name_Province Name_Perfecture Name_County Pinyin Pop_2000 Pop_2010 Pop_2017 Pop_2018 Area Density
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 110101 1101 11 Bei Jing Shi NA Dong Cheng Qu Dongc... 881763 919253 NA 822000 41.8 19670.
2 110102 1101 11 Bei Jing Shi NA Xi Cheng Qu Xiche... 1232823 1243315 NA 1179000 50.5 23360.
3 110114 1101 11 Bei Jing Shi NA Chang Ping Qu Chang... 614821 1660501 NA 2108000 1342 1571.
4 110115 1101 11 Bei Jing Shi NA Da Xing Qu Daxin... 671444 1365112 NA 1796000 1053 1706.
5 110111 1101 11 Bei Jing Shi NA Fang Shan Qu Fangs... 814367 944832 NA 1188000 1995 595.
6 110116 1101 11 Bei Jing Shi NA Huai Rou Qu Huair... 296002 372887 NA 414000 2123 195.
# ... with 1 more variable: geometry

The main data object china is a dataframe, with each row as one county/district level administrative division of China. To plot the map of a particular region, you can filter() codes or names to subset the dataframe. The codes are stored in variables Code_Province, Code_Perfecture, and Code_County, and the names are stored in Name_Province, Name_Perfecture, and Name_County. The codes and names follow the 3-level hierarchy: Province (2-digit), Perfecture (4-digit), and County (6-digit).

Zai Shi Liang Shu Ju chinaZhong ,Xing Zheng Qu Hua De Zhong Wen Ming Yu Dai Ma Jun Fen Wei San Ji :Sheng Ji , Di Ji Yu Xian Ji ,Ke Shi Yong filter()Jin Xing Ren Yi Shai Xuan .

Zhu Yi :Zhi Xia Shi , Te Bie Xing Zheng Qu , Tai Wan Di Qu , Sheng Zhi Guan Shi Yi Ji Qi Ta Te Shu Xing Zheng Qu Yu De Di Ji Qu Yu Ming Cheng Name_PerfectureWei NA. Yong Hu Ke Gen Ju Zuo Tu Xu Yao Jin Xing Xiu Gai .


2. Export the data to shapefile

Jiang Shu Ju Dao Chu Wei Shi Liang Wen Jian

library(sf)
st_write(china, "your/path/to/china.shp", layer_options = "ENCODING=UTF-8")

3. Plotting the population density rank of Beijing, Tianjin, and Hebei

Jing Jin Ji Xian Ji Ren Kou Mi Du Pai Ming Zuo Tu

Since the shapefile data is also a dataframe, it can be plotted by ggplot grammer of graphics. The geometric object is geom_sf().

library(tidyverse)
library(sf)

df <- china %>%
filter(Code_Province %in% c("11","12","13"))

ggplot(data = df) +
geom_sf(aes(fill = rank(Density))) +
scale_fill_distiller(palette = "BuPu", direction = 1) +
theme_bw() +
theme(legend.position = "none")


4. Plotting the map with customized data

Shi Yong Xin Jia Ru De Shu Ju Zuo Tu

New data can be added to the shapefile dataframe as new variables

df$Var <- runif(nrow(df))

ggplot(data = df) +
geom_sf(aes(fill = Var)) +
scale_fill_distiller(palette = "YlOrRd") +
theme_bw() +
theme(legend.position = "none")


5. Plotting the map with random color, but no two adjacent regions have the same color.

Sui Ji Yan Se Zuo Tu Bing Shi De Xiang Lin Qu Yu Yan Se Bu Yi Yang

We use greedy coloring algorithm to solve the problem. The function generate_map_colors() takes a shapefile dataframe as input and outputs a list of index for filling colors.

df2 <- china %>%
filter(Code_Province %in% c("32"))

ggplot(data = df2) +
geom_sf(aes(fill = factor(generate_map_colors(df2)))) +
scale_fill_brewer(palette = "Set3") +
theme_bw() +
theme(legend.position = "none")


6. Plotting the administrative divisions at higher levels (province or perfecture level)

Dui Sheng Di Ji Xing Zheng Qu Zuo Tu

The geometry of county-level divisions can be merged to higher level divisions by functions group_by(), summarise(), and sf::st_union().

Xian Ji Xing Zheng Qu Ke Bei He Bing Wei Di Ji Huo Sheng Ji Xing Zheng Qu Jin Xing Zuo Tu .

df3 <- china %>%
filter(Code_Province %in% as.character(31:36))

df3 <- df3 %>%
group_by(Name_Province) %>%
summarise(geometry = st_union(geometry))

ggplot(data = df3) +
geom_sf(aes(fill = Name_Province)) +
scale_fill_brewer(palette = "Set3") +
theme_bw() +
theme(legend.position = "none")


7. Adding Chinese characters to the map

Zai Di Tu Zhong Jia Ru Yi Zi Biao Ji

To add Chinese characters for the map, R package showtext is required.

if (!require("showtext")) {
install.packages("showtext")

showtext::showtext_auto()

ggplot(data = df3) +
geom_sf(aes(fill = Name_Province)) +
scale_fill_brewer(palette = "Set3") +
theme_bw() +
theme(legend.position = "none") +
geom_sf_label(aes(label = Name_Province))


8. A comprehensive example

Zong He Shi Li :Duo Sheng Di Tu ,An Di Ji Xing Zheng Qu Hua Sui Ji Zhao Se Bing Jia Yi Zi Biao Ji ,Ge Ji Qu Hua Fen Jie Xian Bu Tong

showtext::showtext_auto()

df4 <- china %>%
filter(Code_Province %in% c("32","34"))

df4_prov <- df4 %>%
group_by(Name_Province) %>%
summarise(geometry = st_union(geometry))

df4_perf <- df4 %>%
group_by(Name_Perfecture) %>%
summarise(geometry = st_union(geometry))

ggplot() +
geom_sf(data = df4_perf,
aes(fill = factor(generate_map_colors(df4_perf))),
linetype = "solid", size = 0.5) +
scale_fill_brewer(palette = "Pastel1") +
geom_sf(data = df4, alpha = 0, linetype = "dashed", size = 0.2) +
geom_sf(data = df4_prov, alpha = 0, linetype = "solid", size = 1.2) +
geom_sf_label(data = df4_perf, aes(label = Name_Perfecture)) +
theme_bw() +
theme(legend.position = "none")

About

R Package of Geospatial Shapefile of China Administrative Divisions to the County/District-Level.

Topics

Resources

Readme

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages