2025年3月15日 星期六 甲辰(龙)年 月十四 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > R语言

R语言ggplot2绘制规范的中国地图(省市县三级)

时间:01-04来源:作者:点击数:17

1 如何获取规范的地图矢量数据获取

1.1 AntV L7

可以在该网站下载json格式的地图数据,包括省市县三级的数据以及国界线、海南线等等。数据是符合民政部规范。

1.2 省市县数据CTAmap

2 R语言省市县三级地图可视化

2.1 省级地图可视化

  • library(tidyverse)
  • library(sf)
  • library(ggspatial)
  • library(RColorBrewer)
  • library(sjmisc)
  • ##中国行政区划编码
  • ##数据在公众号玩蛇皮SnakeCharmer回复“教程数据”获取
  • prov = readxl::read_xlsx("./data/china_map/中国行政区划-省.xlsx")
  • prov
  • ##---------------------------------------------------------------
  • ##1.省级地图
  • #数据准备
  • #业务数据
  • ##数据在公众号玩蛇皮SnakeCharmer回复“教程数据”获取
  • data = read_csv("./data/2022分省粮食产量_万吨.csv", locale = locale(encoding = 'gbk'))
  • ##地图区矢量数据
  • china_prov = read_sf("./data/china_map/2023/china_prov.geojson") %>%
  • left_join(data, join_by(name==region)) %>%
  • mutate(prod_lev = cut(product,
  • breaks = c(0, 100, 1000, 2000, 4000, 6000, 8000))) %>%
  • left_join(prov, by="adcode")
  • china_line = read_sf("./data/china_map/2023/china_line.geojson")
  • #作图
  • pal = c("#50b98f", "#92ce6e", "#edda0e", "#e5b107", "#d25c02", "#c01f33")
  • ggplot() +
  • geom_sf(data=china_prov,
  • aes(fill=prod_lev),
  • color = "#011910", linewidth=0.15) +
  • geom_sf(data=china_line,
  • color = "#011910", linewidth=0.5)+
  • geom_sf_text(data = china_prov,
  • aes(label = str_c("\n", short.name)),
  • size=2) +
  • geom_sf_text(data = china_prov,
  • aes(label = round(product)),
  • size=3, nudge_y = 50000) +
  • coord_sf(crs = "+proj=laea +lat_0=40 +lon_0=104") +
  • scale_fill_manual(values = pal,
  • guide = guide_bins(label.position = 'bottom',
  • axis = FALSE,
  • show.limits = TRUE,
  • keywidth = unit(2, 'lines'),
  • keyheight = unit(0.75, 'lines'),
  • nrow = 1),
  • na.value = 'grey95',
  • name = '') +
  • annotation_scale(location = "bl") + #添加比例尺
  • annotation_north_arrow(location = "tl", which_north = "false",
  • style = north_arrow_fancy_orienteering) + #添加指北针
  • theme_minimal() +
  • theme(legend.position = c(0.25, 0.1),
  • legend.direction = 'horizontal',
  • axis.title = element_blank(),
  • plot.title = element_text(size = 22),
  • plot.background = element_rect(fill = "white", color = "white"))

2.2 市级地图可视化

  • ##2.市级地图
  • ##数据在公众号玩蛇皮SnakeCharmer回复“教程数据”获取
  • data = readxl::read_xls("./data/七普人口基本状况.xls")
  • data %>%
  • set_names(nm = names(data) %>% str_remove_all(" |\n")) %>%
  • select(地区, 总人口)-> df
  • china_city = read_sf("./data/china_map/2020/2020年地级/2020年地级.shp") %>%
  • left_join(df, join_by(地名==地区)) %>%
  • mutate(总人口=as.double(总人口)/10000,
  • class = cut(总人口,
  • breaks = c(0, 50, 100, 200, 300, 400, 500, 600, 1000, 1500, 2000, 3206)))
  • ggplot() +
  • geom_sf(data = china_city,
  • aes(fill = class),
  • linewidth=0.075, color = "#241E3E") +
  • geom_sf(data = china_line, fill=NA, linewidth=0.5, color="#241E3E") +
  • coord_sf(crs = "+proj=laea +lat_0=40 +lon_0=104") +
  • scale_fill_manual(values = rev(brewer.pal(11, "Spectral")),
  • name = "",
  • na.value = "grey95") +
  • annotation_scale(location = "bl") + #添加比例尺
  • annotation_north_arrow(location = "tl", which_north = "false",
  • style = north_arrow_fancy_orienteering) + #添加指北针
  • theme_minimal() +
  • theme(plot.background = element_rect(fill = "white", color = NA),
  • legend.position = "bottom",
  • legend.key.height = unit(2, "mm"))

2.3县级地图可视化

  • ##3.县级地图
  • ##业务数据处理
  • rec = "繁昌县=繁昌区;海门市=海门区;监利县=监利市;水城县=水城区;同仁县=同仁市;芜湖县=湾沚区;抚远市=抚远县;else=copy"
  • data %>%
  • set_names(nm = names(data) %>% str_remove_all(" |\n")) %>%
  • filter(!(str_starts(地区, "表")),
  • !is.na(地区),
  • !(地区%in%c("地 区", "市辖区"))) %>%
  • mutate(city = case_when(str_starts(地区, " ") ~ NA,
  • 地区 %in% c("省直辖县级行政区划", "自治区直辖县级行政区划", "县") ~ "prov",
  • .default = 地区)) %>%
  • fill(city, .direction="down") %>%
  • left_join(prov %>% select(name), join_by(city==name), keep = TRUE) %>%
  • fill(name, .direction = "down") %>%
  • filter(str_starts(地区, " ")) %>%
  • left_join(prov, by="name") %>%
  • mutate(地区 = str_remove_all(地区, " "),
  • 地区 = case_when(地区=="市辖区" ~ city,
  • .default = 地区),
  • 地区 = rec(地区, rec=rec),
  • city = if_else(city=="prov", name, city),
  • 总人口 = round(as.double(总人口)/10000),
  • class = cut(总人口,
  • breaks = c(0, 1, 10, 20, 30, 40, 50, 75, 100, 300, 600, 1047),
  • include.lowest = TRUE)) %>%
  • rename(prov.name = name)-> df
  • ##县级地图数据处理
  • china_district = read_sf("./data/china_map/2020/2020年县级/2020年县级.shp") %>%
  • mutate(city = if_else(地级类=="不统计", 省级, 地级)) %>%
  • left_join(df, join_by(地名==地区, city==city), keep=TRUE)
  • ##作图
  • ggplot() +
  • geom_sf(data = china_district,
  • aes(fill = class),
  • linewidth=0.075, color = "#241E3E") +
  • geom_sf(data = china_line, fill=NA, linewidth=0.5, color="#241E3E") +
  • coord_sf(crs = "+proj=laea +lat_0=40 +lon_0=104") +
  • scale_fill_manual(values = rev(brewer.pal(11, "Spectral")),
  • name = "",
  • na.value = "grey95") +
  • annotation_scale(location = "bl") + #添加比例尺
  • annotation_north_arrow(location = "tl", which_north = "false",
  • style = north_arrow_fancy_orienteering) + #添加指北针
  • theme_minimal() +
  • theme(plot.background = element_rect(fill = "white", color = NA),
  • legend.position = "bottom",
  • legend.key.height = unit(2, "mm"))
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
上一篇:手写识别,50行R代码 下一篇:很抱歉没有了
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐