R language ggplot2 draws a map of China and adds river information to Xiaolizi

Today's tweet comes from the submission of the public account reader, the author is RGzxs , and the editing and layout are completed by Xiao Ming.

Below is the text

A few days ago, I read a few maps of China's coastal areas drawn by Mr. Yan (public account: R language data analysis guide), and then tried to draw it myself. The big guy's code only has information about major rivers in China, and no information about regional rivers. In the past few days, I have been looking for information on the Internet to add river information to the map.

Shandong map and Qingdao map download

Because the area to be drawn is the map of the Yellow Sea and Jiaozhou Bay, you need to download a map of Shandong Province and a map of Qingdao. Download address: http://datav.aliyun.com/tools/atlas/index.html#&lat=31.769817845138945&lng=104.29901249999999&zoom=4

web.png

Copy the link of JSON API here, and then use Thunder to download. When downloading the map of Qingdao, first click on the map of Shandong Province, and then click on the map of Qingdao.

set working directory

getwd()
setwd("C:/Users/zxs/Desktop")
getwd()

Load the R package

package.list=c("geoviz","tidyverse","sf","terra","rasterVis","ggspatial",
               "rgdal","rnaturalearth","rnaturalearthdata","raster")

for (package in package.list) {
  if(!require(package,character.only=T,quietly=T)){
    install.packages(package)
    library(package,character.only = T)
  }
  
#头一次这么批量加载R包

Shandong Province Coastal Map

shp1 <- sf::read_sf("shandong.json")
p1<-ggplot()+
  geom_sf(data=shp1,aes(fill=NULL))+
  annotation_scale(location="br")+# 设置距离刻度尺
  #annotation_north_arrow(location="tl",style = north_arrow_nautical(
  #  fill=c("grey40","white"),line_col="grey20"
  #))+
  labs(x=NULL,y=NULL)+
  geom_sf(data=shp1,fill="#AFB3B3",size=0.4,color="gray71")+#添加地图边界
    xlim(118,123)+ylim(34,38.5)+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.title = element_blank())+
  theme(plot.background = element_rect(fill="#9ECFDD",
                                       color="gray71",size = 0.5))+
  annotation_north_arrow(location="tl",
                         style = north_arrow_fancy_orienteering(
                           fill = c("grey40","white"),
                           line_col = "grey20"))
p1

Here, the map range is set by xlim(118,123)+ylim(34,38.5).

image.png

Draw a map of Qingdao

shp2<- sf::read_sf("qingdao.json")
p2<-ggplot()+
  geom_sf(data=shp2,aes(fill=NULL))+
  annotation_scale(location="br")+# 设置距离刻度尺
  annotation_north_arrow(location="tl",style = north_arrow_nautical(
    fill=c("grey40","white"),line_col="grey20"
  ))+
  labs(x=NULL,y=NULL)+
  geom_sf(data=shp2,fill="#AFB3B3",size=0.4,color="gray71")+#添加地图边界
  xlim(120,120.6)+ylim(35.8,36.5)+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.title = element_blank())+
  theme(plot.background = element_rect(fill="#9ECFDD",
                                       color="gray71",size = 0.5))+
  annotation_north_arrow(location="tl",
                         style = north_arrow_fancy_orienteering(
                           fill = c("grey40","white"),
                           line_col = "grey20"))
p2
image.png

Download the river information website

National Basic Geographic Information Center

https://www.webmap.cn/mapDataAction.do?method=forw&resType=5&storeId=2&storeName=%E5%9B%BD%E5%AE%B6%E5%9F%BA%E7%A1%80%E5%9C%B0%E7%90%86%E4%BF%A1%E6%81%AF%E4%B8%AD%E5%BF%83&fileId=1A5CEBDB34C04A29AAB7E673930498E7

Website interface (the website requires registration)

Add river information

river1<-sf::read_sf("/j51c004001/j51c004001/hydl.shp")
p3<-ggplot()+
  geom_sf(data=shp2,aes(fill=NULL))+
  annotation_scale(location="br")+# 设置距离刻度尺
  annotation_north_arrow(location="tl",style = north_arrow_nautical(
    fill=c("grey40","white"),line_col="grey20"
  ))+
  labs(x=NULL,y=NULL)+
  geom_sf(data=shp2,fill="#AFB3B3",size=0.4,color="gray71")+#添加地图边界
  xlim(120,120.6)+ylim(35.8,36.5)+
  geom_sf(data = river1,col='#87CEEB',size=0.5)+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.title = element_blank())+
  theme(plot.background = element_rect(fill="#9ECFDD",
                                       color="gray71",size = 0.5))+
  annotation_north_arrow(location="tl",
                         style = north_arrow_fancy_orienteering(
                           fill = c("grey40","white"),
                           line_col = "grey20"))
p3
image.png

Now draw all the rivers in the Jiaozhou Bay area on the map, we don't need so many, we need to filter the rivers

river1<-sf::read_sf("j51c004001/j51c004001/hydl.shp")
#筛选目标河流
river12<-filter(river1,river1$NAME=="大沽河")

p4<-ggplot()+
  geom_sf(data=shp2,aes(fill=NULL))+
  annotation_scale(location="br")+# 设置距离刻度尺
  annotation_north_arrow(location="tl",style = north_arrow_nautical(
    fill=c("grey40","white"),line_col="grey20"
  ))+
  labs(x=NULL,y=NULL)+
  geom_sf(data=shp2,fill="#AFB3B3",size=0.4,color="gray71")+#添加地图边界
  xlim(120,120.6)+ylim(35.8,36.5)+
  geom_sf(data = river12,col='#87CEEB',size=0.5)+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.title = element_blank())+
  theme(plot.background = element_rect(fill="#9ECFDD",
                                       color="gray71",size = 0.5))+
  annotation_north_arrow(location="tl",
                         style = north_arrow_fancy_orienteering(
                           fill = c("grey40","white"),
                           line_col = "grey20"))
p4
image.png

Finally the puzzle

library(patchwork)
p3+p4
image.png

欢迎大家关注我的公众号

小明的数据分析笔记本

今天推文的示例数据和代码可以在公众号后台留言 20210901 获取(精确匹配开头结尾都不能有空格)

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!


本文分享自微信公众号 - 小明的数据分析笔记本(gh_0c8895f349d3)。
如有侵权,请联系 [email protected] 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4579431/blog/5219729