ggspatial | ggplot2的地图制作拓展包(1):如何添加指北针和比例尺

在前面的推文里,小编介绍了R语言的基础绘图系统和tmap工具包绘制地图的方法。ggplot2工具包作为现在最受欢迎的绘图包,其本身绘制地图的功能并不算强大。但是ggplot2有许多优秀的拓展包,它们共同构建了ggplot2绘图系统,ggspatial就是地图绘制方面的一个很常用的拓展包。

ggspatial工具包的函数并不多,其主要作用就是弥补ggplot2绘制地图方面的不足。比如,ggplot2包的作者没有开发给地图添加指北针和比例尺的函数,而在专业的地图绘制中,这二者又必不可少。

library(ggplot2)
library(ggspatial)
library(patchwork)

在示例数据选取方面,小编尽量使用系统中自带的数据,而R语言系统中涉及中国的地理数据一般都属于“问题地图”,因此在这里使用美国的地理数据作为示例数据。

示例数据加载方式如下:

library(tidyverse)
data <- socviz::county_data
albersusa::counties_sf(proj = "laea") %>%
  mutate(fips = as.character(fips)) %>%
  left_join(data, by = c("fips" = "id")) -> usa
  • 注:socvizalbersusa两个工具包都需要通过github安装,不会安装的读者可以直接在后台输入关键词“示例数据”获取示例数据。

本篇推文一共介绍5个函数:

  • annotation_north_arrow:添加指北针

  • annotation_scale:添加比例尺

  • annotation_map_tile:添加背景地图

  • annotation_spatial_hline:添加水平线(纬线)

  • annotation_spatial_vline:添加垂直线(经线)

从函数名就可以看出,这些函数均属于ggplot2绘图系统的注释类函数。

指北针

添加指北针的函数语法结构如下:

annotation_north_arrow(
  mapping = NULL,
  data = NULL,
  ...,
  height = unit(1.5, "cm"),
  width = unit(1.5, "cm"),
  pad_x = unit(0.25, "cm"),
  pad_y = unit(0.25, "cm"),
  rotation = NULL,
  style = north_arrow_orienteering
)
  • hegiht、width:控制指北针尺寸的参数;

  • pad_x、pad_y:控制指北针绝对位置的参数;

  • style:指北针的样式;

  • 除上述参数外,该函数还有两个aesthetic类参数:which_north、location,分别控制指北针箭头的方向和指北针的相对位置,具体见下文示例。

默认状态的效果:

p <- ggplot(usa) +
  geom_sf(color = NA, fill = "lightblue")

p + annotation_north_arrow()
073a149ff16b4410ebe5507e95ca21c3.png

位置调整:

  • pad_xpad_y调整绝对位置;

  • location调整相对位置:t(top)、b(bottom)、l(left)、r(right)。

p + annotation_north_arrow(
  pad_x = unit(10, "cm"))

p + annotation_north_arrow(location = "br")

p + annotation_north_arrow(location = "br",
                           pad_x = unit(1, "cm"))
639c4dd715e8c6642ec452df564d3a8f.png 8b8487d20df35910cc473502d7ccc410.png 09e96a0f8fb4b8fb4a381617502eb403.png
  • 绝对位置参数的效果会受到地图输出尺寸的影响;

  • 绝对位置和相对位置参数可以同时使用,即可以在相对位置的基础上使用绝对位置进行微调(图3)。

指北针方向:

  • which_north参数默认设置为"grid",表示指北针始终垂直向上;

  • which_north参数若设置为"true",表示指北针方向与其所在位置的纬线保持一致。

p + annotation_north_arrow(which_north = "true")
d4afdba6173064b42b1258ddc3605cfa.png

指北针的类型:

  • north_arrow_orienteering

  • north_arrow_fancy_orienteering

  • north_arrow_minimal

  • north_arrow_nautical

上述每种类型都对应一个函数,可以调用其参数对形态进行调整,以其中一个为例:

north_arrow_fancy_orienteering(
  line_width = 1,
  line_col = "black",
  fill = c("white", "black"),
  text_col = "black",
  text_family = "",
  text_face = NULL,
  text_size = 10,
  text_angle = 0
)
p + annotation_north_arrow(
  style = north_arrow_fancy_orienteering(text_family = "mono",
                                         text_face = "bold"))
465e4c70830c81da1acce08eab29efeb.png

比例尺

添加比例尺的函数语法结构如下:

annotation_scale(
  mapping = NULL,
  data = NULL,
  ...,
  plot_unit = NULL,
  bar_cols = c("black", "white"),
  line_width = 1,
  height = unit(0.25, "cm"),
  pad_x = unit(0.25, "cm"),
  pad_y = unit(0.25, "cm"),
  text_pad = unit(0.15, "cm"),
  text_cex = 0.7,
  text_face = NULL,
  text_family = "",
  tick_height = 0.6
)
  • bar_cols:比例尺中交替出现的颜色;

  • text_*:控制比例尺注释文本的参数;

  • aesthetic类参数:

    • width_hint:比例尺长度占地图宽度的比例;

    • unit_category:比例尺单位类型;"metric"表示公制单位(默认),"imperial"表示英制单位;

    • style:比例尺的类型,可选项有"bar"(默认)、"ticks";

    • location:比例尺相对位置;

    • line_col、text_col:线条、文本颜色。

默认效果:

p + annotation_scale()
bbca5a7bf51b3f695881828fa644073a.png

调整部分参数后的效果:

p + annotation_scale(width_hint = 0.4,
                     unit_category = "imperial",
                     style = "ticks",
                     line_col = "red",
                     pad_y = unit(0.5, "cm"))

f23dc68e455263ae4cbbe9431b669483.png

背景地图

添加背景地图的函数语法结构如下:

annotation_map_tile(
  type = "osm",
  zoom = NULL, zoomin = -2,
  forcedownload = FALSE,
  cachedir = NULL,
  progress = c("text", "none"),
  quiet = TRUE, interpolate = TRUE,
  data = NULL,
  mapping = NULL, alpha = 1
)
  • type:背景地图类型;通过rosm包的osm.types()函数可查看所有可用类型;默认为;

  • zoom:背景地图的精细程度;数值越大精细度越高。

查看所有可用背景地图类型:

rosm::osm.types()
##  [1] "osm"                    "opencycle"              "hotstyle"              
##  [4] "loviniahike"            "loviniacycle"           "hikebike"              
##  [7] "hillshade"              "osmgrayscale"           "stamenbw"              
## [10] "stamenwatercolor"       "osmtransport"           "thunderforestlandscape"
## [13] "thunderforestoutdoors"  "cartodark"              "cartolight"
ggplot(usa) +
  geom_sf() +
  annotation_map_tile(zoom = 5)
9f0a28d60016496384897417257213c5.png

水平/垂直线

添加水平线的函数是annotation_spatial_hline(),添加垂直线的函数是annotation_spatial_vline()。这两个函数的语法结构是一样的,以前者为例:

annotation_spatial_hline(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  ...,
  intercept = waiver(),
  limits = NULL,
  detail = 100,
  crs = NULL,
  na.rm = FALSE,
  show.legend = NA
)
  • intercept:水平(垂直)位置;

  • limits:垂直(水平)范围;

  • crs:地理或投影坐标系。

其实更准确地说,这两个函数添加的分别是纬线和经线,因为当地理尺度较大且未投影时,它们绘制出并非是直线。

ggplot(usa) +
  geom_sf(color = NA, fill = "lightblue") +
  annotation_scale() +
  annotation_spatial_hline(intercept = 33, 
                           col = "red",
                           limits = c(-120, -60))
cfb58a511f5df2276bee8555fa60a569.png
  • 本例中的crs参数默认值为4326,即未进行投影;

  • intercept = 33表示绘制北纬33度线;

  • limits = c(-120, -60)表示在绘制纬线时,经度的范围是西经120度到西经60度。

  • 1
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值