R 地图绘制-比例尺与指北针

2022-10-25 14:52:14 浏览数 (1)

ggplot绘制map

R语言可以进行数据分析,也可以进行地图绘制,而且非常简洁,快速。 虽然Arcgis基于桌面可视化操作,能够进行空间分析,但是唯一不足的就是操作步骤繁琐而且一不小心,就要从头再来,可重复性较低。

这篇文章主要讲述如何利用R语言中的ggplotsf绘制带有指北针、图列与标尺的地图

屏幕快照 2020-06-28 下午9.27.59.png

数据

我们下载非洲地区54个国家的图层Afirca.json保存在本地。然后加载包去读取。然后在ggplot中使用 geom_sf来简单画出非洲地区的轮廓

代码语言:javascript复制
library(tidyverse)
library(sf)
library(ggspatial)
library(ggthemes)
# https://geojson-maps.ash.ms/
africa <- read_sf("africa.json")

# plot
africa %>% 
  ggplot()   
  geom_sf()
 
# populations with colors
ggplot(africa)  
  geom_sf(aes(geometry = geometry, fill = pop_est))

屏幕快照 2020-06-29 上午1.48.20.png

指北针

一张标准的地图需要有比例尺,地图及指北针,所以这次我们加上比例尺与指北针,通过ggspatial包,调用annotation_scale来增加比例尺。location = "bl"是调整比例尺位置, width_hint = 0.3调整比例的长度。 annotation_north_arrow则是用来添加指北针。style =north_arrow_nautical来更改指北针的类型,主要有north_arrow_orienteeringnorth_arrow_fancy_orienteeringnorth_arrow_minimalnorth_arrow_nautical四种类型,其他参数可help(annotation_north_arrow)查看。

代码语言:javascript复制
# with scales and north_arrow
ggplot(africa)  
  geom_sf(aes(geometry = geometry, fill = pop_est))  
  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) 

屏幕快照 2020-06-29 上午1.37.08.png

背景调整

如果不喜欢有grid的背景,可以根据喜好,调节自己喜欢的背景 这里我们采用, theme_light()白色网格背景。更多背景设置,请见ggplot2 主题背景设置

代码语言:javascript复制
# with blank themes
ggplot(africa)  
  geom_sf(aes(geometry = geometry, fill = pop_est))  
  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)  
  theme_light()

屏幕快照 2020-06-29 上午1.48.33.png

图例颜色

默认的图例颜色是blue色调,我们可以根据 来更改红色基准的色调。 legend 是默认的分段方式,我们可以根据需要设定成4分类,或者更改图例的距离。

代码语言:javascript复制
# with colors
ggplot(africa)  
  geom_sf(aes(geometry = geometry, fill = pop_est))  
  scale_fill_gradientn(colours=brewer.pal(5,"Reds")) 
  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)  
  theme_light()

# for 4 categories
africa %>% mutate( new=cut(pop_est,b = 4)) %>% 
ggplot()  
  geom_sf(aes(geometry = geometry, fill = new))  
  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)  
  theme_light()
  
 # set for sacles

my_breaks = c(0,3207562,10401245,18419935,20644434,149229090 )
ggplot(africa)  
  geom_sf(aes(geometry = geometry, fill = pop_est))  
  scale_fill_gradient(name = "count", 
                        breaks = my_breaks, labels = my_breaks)
                        
#
ggplot(africa)  
  geom_sf(aes(geometry = geometry, fill = pop_est))  
  scale_fill_gradient(name = "count", 
                        breaks = my_breaks, labels = my_breaks, guide="legend")

屏幕快照 2020-06-29 上午1.47.58.png

屏幕快照 2020-06-29 上午1.48.05.png

关于更多颜色设置ggplot2: Elegant Graphics for Data Analysis legend分类legend and labels

参考

  1. Geospatial Visualization
  2. sf 与指北针
  3. geocompkg | metapackage for the Gecomputation with R book
  4. This repository contains the source for the book Geospatial Visualization.
  5. Chapter 8: Making maps with R
  6. ggplot2 主题背景设置

0 人点赞