在R中绘制世界地图并标注采样点并不是一件难事,目前也有不少的R包提供了实现方法,如map,tmap等R包。因为前段时间有类似的作图需求,所以我也去研究了一下。虽然在R中绘制采样地图的方法已经比较完善,不过绘制世界地图的投影基本上就是经典的WGS84(世界地理坐标系统,EPSG:4326),并没有我们在一些顶刊中经常见到的robinson投影风格的采样地图。基于此,我开发了smap包,其大体过程是:通过rnaturalearth获取地图,sf包实现投影转换,ggplot2包完成绘图。
在介绍smap包的具体功能前,首先给大家展示一下该包的可视化效果。我将smap包提交到了github上,大家有需要的可以使用devtools或remotes包进行安装,如果安装不了的话也可直接使用我放在github上的源代码(https://github.com/biodiversity-monitoring/smap)。
remotes::install_github("biodiversity-monitoring/smap")
library(smap)
smap包主要是为了绘制robinson投影风格的采样地图而开发的,因此功能比较单一,主要包含了plot_map(),plot_sampling_points()和plot_biomes_map()三个函数。函数的详细参数可以参考我在R中提交的说明文档。该函数主要用于绘制世界地图,参数主要是draw_countries,draw_coastline和fill_color三个,用于决定是否绘制国界和海岸线,以及使用的填充颜色。# 绘制显示国家边界的世界地图
plot_map(draw_countries = TRUE, draw_coastline = FALSE,fill_color = "#e0e0eb")
# 绘制只显示海岸线而不显示国界的世界地图
plot_map(draw_countries = FALSE, draw_coastline = TRUE,fill_color = "#B0E2FF")
# 绘制既无国界也无海岸线的世界地图
plot_map(draw_countries = FALSE, draw_coastline = FALSE,fill_color = "#A6D96A")
n <- 5
x <- runif(n, min = 0, max = 100)
y <- runif(n, min = 0, max = 50)
site <- paste0("Plot ", 1:n)
sampling_points <- data.frame(site = site, x = x, y = y)
p <- plot_map(fill_color = "gray", draw_countries = FALSE, draw_coastline = FALSE)
plot_with_points <- plot_sampling_points(p, sampling_points)
plot_with_points
该函数主要用于绘制包含生物群落的世界地图,生物群落数据需要自已去https://ecoregions.appspot.com/下载。(我本来想将这个数据集内置到R包中去的,但提交时造成了新的bug,而且数据本身较大也不适合上传,所以暂时放弃了这个想法。)library(sf)
# 加载生物群落数据,可从https://ecoregions.appspot.com/下载
biomes <- st_read("Ecoregions2017.shp")
# 为生物群落定义自定义颜色
biome_colors <- c(
"Boreal Forests/Taiga" = "#7AB6F5",
"Deserts & Xeric Shrublands" = "#CC6767",
"Flooded Grasslands & Savannas" = "#BEE7FF",
"Mangroves" = "#FE01C4",
"Mediterranean Forests, Woodlands & Scrub" = "#FE0000",
"Montane Grasslands & Shrublands" = "#D6C39D",
"Temperate Broadleaf & Mixed Forests" = "#00734C",
"Temperate Conifer Forests" = "#458970",
"Temperate Grasslands, Savannas & Shrublands" = "#FEFF73",
"Tropical & Subtropical Coniferous Forests" = "#88CE66",
"Tropical & Subtropical Dry Broadleaf Forests" = "#CCCD65",
"Tropical & Subtropical Grasslands, Savannas & Shrublands" = "#FEAA01",
"Tropical & Subtropical Moist Broadleaf Forests" = "#38A700",
"Tundra" = "#9ED7C2"
)
# 绘生物群落地图
plot_biomes_map(biomes, biome_colors, draw_countries = FALSE, draw_coastline = TRUE, projection = "+proj=robin")
整个smap包其实非常简单,本质上其实就是投影的转换问题。写成R包的形式也只是一种尝试,如有不当之处,欢迎大家批评指正!