前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ggplot增设小地图(南海九段线)

ggplot增设小地图(南海九段线)

作者头像
Jamesjin63
发布2022-11-03 14:49:45
1.3K0
发布2022-11-03 14:49:45
举报
文章被收录于专栏:EpiHubEpiHub

toc

ggplot增设小地图(南海九段线)

背景

用Arcgis专业作图工作制作中国地图时候,往往会添加南海九段线,其中南海九段线是因为需要保证中国土地的主权完整性。在Arcgis中操作时候,会根据标准中国地图,实现增加第二个图层,然后只截取南海部分,完成两个图层展现在同一副图中。

那么问题来了,如何在R中实现该操作?

现在绘制地图经常会用到ggplotsf,如何实现同一副地图中,添加南海九段线呢。

主要是借助于cowplot包,可以实现两个图层的叠加。

ps:需要确保中国地图来源的权威性,关于如何获取正确,官方地图文件,点击这里

接下来,根据案例展示中国地图,南海九段线绘制。

案例实现

获取中国地图,然后绘制两个图层,plot_china与china_mini.需要注意的是

  • china_mini的选取,借助coord_sf可选定范围
  • draw_plot可以将china_mini插入到主图层,需要调整参数位置。
代码语言:javascript
复制
library(cowplot)
library(tidyverse)
library(ggspatial)
library(sf)
china = read_sf("https://geo.datav.aliyun.com/areas_v2/bound/100000_full.json")

# Map of china
(plot_china = 
    ggplot(china)+
    geom_sf())

# china_mini map
(china_mini <- ggplot(data = china) +
    geom_sf(fill = "cornsilk") +
    coord_sf( xlim = c(105, 125),
              ylim = c(4,26), 
              expand = FALSE, datum = NA)+
    theme_few())

# Place the inset
ggdraw() +
  draw_plot(plot_china) +
  draw_plot(china_mini, x = 0.68, y = 0.05, width = .25, height = .25)

# Save the map
ggsave(filename = "map_inset.png", 
       plot = map_with_inset, 
       width = 10,
       height = 10,
       units = "cm",
       dpi = 100)

image.png

更改主题

上述china地图的背景不是很好,有网格,如果需要去除网格,或者更改其他主题。请借助

ggthemes提供的主题。可自行探索。

image.png

插入图例与指南针

有时候需要插入比例尺与指南针,见 R 地图绘制-比例尺与指北针

这里直接给出code。

代码语言:javascript
复制
# Map of china with scale and north
(plot_china = 
    ggplot(china)+
    geom_sf()+
    annotation_scale(location = "bl", width_hint = 0.4) +
    annotation_north_arrow(location = "tr", which_north = "true", 
                           pad_x = unit(0.05, "in"), pad_y = unit(0.05, "in"),
                           style = north_arrow_nautical)
    )

# Place the inset
ggdraw() +
  draw_plot(plot_china) +
  draw_plot(china_mini, x = 0.68, y = 0.05, width = .25, height = .25)

image.png

结语

cowplot使ggplot画图越来越便捷。可以叠加图层。从而实现ggplot多图层操作。

南海九段线与中国全国领土一定要在图层中表现出来,不然绘制的中国地图,有什么意义。不管是做学术,还是在网上发布例子,请保证国家的完整性,希望以后搜索的都是一个完整的中国。

参考

  1. ggplot2(六)|套用主题模板
  2. Introduction to cowplot
  3. Create an inset map in R
  4. Drawing beautiful maps programmatically with R, sf and ggplot2 — Part 3: Layouts
  5. ggplot 与sf 实现地图缩放功能
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ggplot增设小地图(南海九段线)
    • 背景
      • 案例实现
        • 更改主题
          • 插入图例与指南针
            • 结语
            • 参考
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档