前面我们爬取民政部官网的行政区域代码,这里我们根据爬取县级地图数据绘制市级地图
1 需要的包
rm(list = ls()); gc() # 清空内存library(tibble)library(dplyr)library(purrr)library(magrittr)library(sf)library(stringr)library(tidyr)library(readr)library(ggplot2)library(rmapshaper) # 拓扑融合行政区域## used (Mb) gc trigger (Mb) max used (Mb)## Ncells 524652 28.1 1188345 63.5 621320 33.2## Vcells 1006270 7.7 8388608 64.0 1599914 12.3
2 行政区域代码
爬取行政区域代码见笔者前面的文章。
file_pre sheng_shi_xian_code "sheng_shi_xian_code.csv", sep = "/"), header = TRUE, stringsAsFactors = FALSE)# 动态表格展示DT::datatable(sheng_shi_xian_code)
3 市内县级数据
这里我们以获取成都市县级地图为例。
3.1 获取json
数据
# 筛选成都市行政区域代码code_chengdu % filter(sheng == "四川省" & shi == "成都市") %>% mutate(code = as.character(code_x), code_s2 = as.character(code_s)) %>% select(shi, code_s = code_s2, xian, code)head(code_chengdu)# API前缀API_pre # 通过API获取成都市县级地图数据mapdata_chengdu unique(code_chengdu$code_s), ".json"), stringsAsFactors=FALSE)## Reading layer `510100' from data source `http://xzqh.mca.gov.cn/data/510100.json' using driver `GeoJSON'## Simple feature collection with 28 features and 4 fields## geometry type: POLYGON## dimension: XY## bbox: xmin: 103.0995 ymin: 30.19739 xmax: 105.1024 ymax: 31.60111## epsg (SRID): NA## proj4string: NA
3.2 数据处理
包括筛选数据,去除毗邻行政区域的数据。计算行政区域质心坐标。
# 筛选数据mapdata_chengdu %<>% select(-id, -FillColor) %>% rename(code = QUHUADAIMA, name = NAME) %>% filter(code %in% code_chengdu$code)# 计算行政区域质心坐标centerPoints_chengdu % select(-name) %>% # 计算行政区域质心坐标 st_centroid() %>% left_join(code_chengdu, by = "code") %>% select(-shi, -code_s)
4.3 画图
ggplot(mapdata_chengdu) + geom_sf(aes(fill = code), show.legend = FALSE) + # geom_sf_text(data = centerPoints_chengdu, aes(label = xian), color = "black") + coord_sf()
3.4 业务数据
如果有业务数据,直接将行政区域地图数据(sf
格式)与业务数据联联结表,再将中心点坐标数据(sf
格式)与业务数据联结表,然后拼接字符串,拼接行政区域名称和业务数据变量,使用\n
换行符作为分隔符。这样就能绘制业务数据地图了。如下图,笔者绘制的金华市快递复工复产地图。
4 省内市级数据
这里我们首先获取省内各个市的县级数据,然后熔化掉市内县级行政区域的边界线,就得到省内市级数据。如果需要省内县级数据,则不进行边界线熔化这一步。
4.1 自定义函数
# 筛选四川省行政区域代码code_sichuan % filter(sheng == "四川省") %>% mutate(code_s2 = as.character(code_she), code_s3 = as.character(code_s), code = as.character(code_x)) %>% select(sheng, code_she = code_s2, shi, code_s = code_s3, xian, code_x = code) head(code_sichuan) # 自定义一个函数,获取每个市的县级地图数据get_xian data_sf stringsAsFactors=FALSE) %>% select(xian = NAME, code_x = QUHUADAIMA, geometry) %>% filter(code_x %in% code_range)return(data_sf)}
4.2 获取json
数据
通过map
函数遍历所有省内市的json
数据。然后进行熔化掉市内县级行政区的边界线.
# 获取四川省县级地图数据code_sichuan2 %select(shi, code_s) %>% distinct(code_s, .keep_all = TRUE)mapdata_sichuan %select(code_s, code_x) %>% group_nest(code_s) %>% left_join(code_sichuan2, by = "code_s") %>% mutate(., code_range = map(.$data, ~.x$code_x)) %>% select(shi, code_s, code_range) %>% # 获取json地图数据 mutate(., xian_sf = map2(.x = .$code_s, .y = .$code_range, ~get_xian(.x, .y))) %>% mutate(., xian_sf2 = map(.$xian_sf, ~select(.x, -xian))) %>% select(shi, code_s, xian_sf2) %>% # 熔化市内县级行政区的边界线 mutate(., data = map(.$xian_sf2, ~ms_dissolve(.x))) %>% mutate(., shi_sf = map(.$data, ~.x$geometry)) %>% select(shi, code_s, shi_sf)# 合并sfcmapdata_sichuan2 % purrr::reduce(., c) %>% st_set_geometry(select(mapdata_sichuan, -shi_sf), .)
ggplot(mapdata_sichuan2) + geom_sf(aes(fill = shi)) + coord_sf()
上图中,绵阳地区有个漏洞,应该是数据源问题,暂且忽略,这里只关注计算过程。
4.3 计算质心坐标
# 计算行政区域质心坐标centerPoints_sichuan % select(-shi) %>% # 计算行政区域质心坐标 st_centroid() %>% left_join(code_sichuan2, by = "code_s") %>% select(shi, code_s, geometry)
ggplot(mapdata_sichuan2) + geom_sf(aes(fill = code_s), show.legend = FALSE) + geom_sf_text(data = centerPoints_sichuan, aes(label = shi), color = "black") + coord_sf()
参考来源:
中国县级地图绘制
全国行政区划信息查询平台
浙江行政区域代码表格
2019年12月中华人民共和国县以上行政区划代码
百度地图坐标拾取系统
计算行政区域中心坐标
行合并sf对象
高德地图API接口获取经纬度坐标
Geocomputation with R
rmapshaper包github
![f1f4528df3a74c09098cdab36180666a.png](https://i-blog.csdnimg.cn/blog_migrate/5d0b47db2c715abffde96e3414c0f793.jpeg)
如需联系EasyShu团队
请加微信:EasyCharts
微信公众号【EasyShu】博文代码集合地址
https://github.com/Easy-Shu/EasyShu-WeChat
《R语言数据可视化之美》增强版
增强版配套源代码下载地址
Github
https://github.com/Easy-Shu/Beautiful-Visualization-with-R
百度云下载
https://pan.baidu.com/s/1ZBKQCXW9TDnpM_GKRolZ0w
提取码:jpou