tmap是用來繪製地圖的R程序包,作者是荷蘭統計學家Martijn Tennekes (https://github.com/mtennekes/tmap)。tmap結合sf、sp等程序包,繪製設色地圖、點分布圖、柵格圖等十分方便,本公眾號以前也有介紹。
不過,tmap在繪製複雜的採樣點分布圖時,默認的圖例往往不太理想。tmap用點的形狀、顏色分別表示採樣點的類型(如森林、溼地、草原),用大小表示數量(物種豐富度等),此時,形狀、顏色和大小三個維度是分開的,圖例也默認相應分開,而這並不是用戶想要的,因此需要對代碼進行修改。
本文給出一些示例代碼,主要參考Xu S. et al. (2020)論文和附件。相應數據和代碼可在 https://github.com/helixcn/ecoinformatics 下載。
讀取數據圖中需要有指北針和比例尺,要求用藍色實心三角,表示森林(forest);綠色實心圓點,表示草地(grassland);藕荷色實心菱形,表示溼地(wetland)。
setwd("/Users/admin/Documents/")
library(tmap)
library(tmaptools)
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(sp)
library(openxlsx)
rm(list = ls()) # 刪除工作空間中原來的對象
sites <- read.xlsx("rspb20202063_si_002.xlsx")
head(sites)
## Variable References Country Latitude Longitude
## 1 Aboveground biomass Liu et al. 2017b Germany 51.33857 12.378462000000001
## 2 Aboveground biomass Liu et al. 2017b Germany 51.33857 12.378462000000001
## 3 Belowground biomass Brassard et al. 2011 Canada 49.50000 -89.6
## 4 Belowground biomass Brassard et al. 2011 Canada 49.17000 -89.83
## 5 Belowground biomass Finér et al. 2017 Spain 40.70000 −1.9
## 6 Belowground biomass Finér et al. 2017 Spain 40.70000 −1.9
## MAT.(℃) MAP.(mm) Ecosystem Duration Plant.diversity.level Plot.area.(m2)
## 1 9.545 544 forest 0.29 2 0.0881
## 2 9.545 544 forest 0.29 4 0.0881
## 3 2.500 712 forest 1.00 NA 400.0000
## 4 0.600 823 forest 1.00 NA 400.0000
## 5 10.200 499 forest NA 2 900.0000
## 6 10.200 499 forest NA 3 900.0000
## LnRR
## 1 0.06509796
## 2 0.31643103
## 3 0.53959844
## 4 0.32157929
## 5 -0.36161104
## 6 -0.62003058
sites$Longitude <- as.numeric(sites$Longitude) # 某些Longitude或者Latitude記錄可能為空,不能識別為numeric類型,因此先要用as.numeric轉換。
## Warning: NAs introduced by coercion
sites$Latitude <- as.numeric(sites$Latitude)
sites <- sites[!(is.na(sites$Longitude)|is.na(sites$Latitude)),] # 去掉Longitude和latitude為NA的記錄
# 建立spatial data.frame
coordinates(sites) <- ~Longitude+Latitude
proj4string(sites) <- CRS("+proj=longlat +datum=WGS84") # 設定WGS84投影
head(sites)
## class : SpatialPointsDataFrame
## features : 6
## extent : -89.83, 26, 47.3, 51.33857 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## variables : 10
## names : Variable, References, Country, MAT.(℃), MAP.(mm), Ecosystem, Duration, Plant.diversity.level, Plot.area.(m2), LnRR
## min values : Aboveground biomass, Brassard et al. 2011, Canada, 0.6, 544, forest, 0.29, 2, 0.0881, -0.217864917184295
## max values : Belowground biomass, Liu et al. 2017b, Romania, 9.54499998254081, 823, forest, 1, 4, 900, 0.539598438991999
world <- read_sf("world20200121_polygon.shp") #讀取shape文件(注意:本圖為多邊形,沒有九段線)
st_crs(world) <- 4326 # 確定WGS84投影
默認情況下,地圖的圖例有三種,分別按照顏色、形狀和大小給出。
## 01
map01 <- tm_shape(world) +
tm_borders("grey60", lwd = 1) + # 國界線為灰色
tm_shape(sites) +
tm_symbols(shape= "Ecosystem",
alpha = 1,
col = "Ecosystem", # 點的顏色按照Ecosystem顯示
border.lwd = 2,
palette = c("blue", "seagreen4", "magenta2"), # 指定三種顏色
shapes = c(17, 19, 18), # 點的形狀按照Ecosystem顯示,分別為17、19、18
size = 0.3,
labels = c("forest", "grassland", "wetland"), # 點的名稱設定為forest、grassland、wetland
title.col = "Ecosystem", # 圖例名稱為Ecosystem
legend.col.show = TRUE, # 在圖例中顯示顏色
legend.shape.show = TRUE) + # 在圖例中顯示形狀
tm_scale_bar(breaks = c(0, 2500, 5000, 10000), # 比例尺
position=c(0.60, 0.0), # 比例尺在地圖中的位置
text.size = 1) +
tm_compass(type = "4star", # 指北針類型
position=c("right", "top"), # 指北針位置
size = 2.5) + # 指北針大小
tm_layout(inner.margins=c(0.12,0.03,0.08,0.03), # 地圖邊緣的大小
legend.stack = "horizontal", # 圖例排列
fontfamily = "FreeSerif") + # Times New Roman (MacOS)
tm_legend(position=c(0.05, 0.20), # 圖例放在圖左下角
legend.text.size = 0.8,
legend.title.size = 1.2) +
tm_add_legend(
type = "symbol",
title = "Ecosystem" # 圖例標題
)
map01
## Scale bar set for latitude km and will be different at the top and bottom of the map.
## 8inches by 5 inches, 600dpi
# jpeg("map01.jpeg", width = 8*600, height = 5*600, res = 600)
# map01
# dev.off()
用tm_add_legend手工調整:
tm_add_legend(
type = "symbol",
labels = c("grassland", "forest", "wetland"),
col = c("seagreen4", "blue", "magenta2"),
shape = c(19, 17, 18),
title = "Ecosystem"
)
更改後的代碼:
# 02
map02 <- tm_shape(world) +
tm_borders("grey60", lwd = 1) +
tm_shape(sites) +
tm_symbols(shape= "Ecosystem",
alpha = 1,
col = "Ecosystem",
border.lwd = 2,
palette = c("blue", "seagreen4", "magenta2"),
shapes = c(17, 19, 18),
size = 0.3,
labels = c("forest", "grassland", "wetland"),
title.col = "Ecosystem",
legend.col.show = FALSE,
legend.shape.show = FALSE) +
tm_scale_bar(breaks = c(0, 2500, 5000, 10000),
position=c(0.60, 0.0),
text.size = 1) +
tm_compass(type = "4star",
position=c("right", "top"),
size = 2.5) +
tm_layout(inner.margins=c(0.12,0.03,0.08,0.03),
legend.stack = "horizontal",
fontfamily = "serif") +
tm_legend(position=c(0.05, 0.20),
legend.text.size = 0.8,
legend.title.size = 1.2) +
tm_add_legend(
type = "symbol",
labels = c("grassland", "forest", "wetland"), # 圖例的標籤
col = c("seagreen4", "blue", "magenta2"), # 圖例顏色
shape = c(19, 17, 18), # 圖例
title = "Ecosystem"
)
map02
## Scale bar set for latitude km and will be different at the top and bottom of the map.
## 8inches by 5 inches, 600dpi
# jpeg("map02.jpeg", width = 8*600, height = 5*600, res = 600)
# map02
# dev.off()
# 03
map03 <- tm_shape(world,
xlim = c(-15, 40), # 經度範圍
ylim = c(35, 70)) + # 緯度範圍
tm_borders("grey60", lwd = 1) +
tm_shape(sites) +
tm_symbols(shape= "Ecosystem",
col = "Ecosystem",
palette = c("blue", "seagreen4", "magenta2"),
size = 0.2,
shapes = c(17, 19, 18),
border.lwd = 1.8) +
tm_scale_bar(position = c("left", "top")) +
tm_layout(legend.show = FALSE, fontfamily = "serif")
map03
## Scale bar set for latitude km and will be different at the top and bottom of the map.
## 4 inches by 5 inches, 600dpi
# jpeg("map03.jpeg", width = 4*400, height = 5*400, res = 600)
# map03
# dev.off()
Xu, S., Eisenhauer, N., Ferlian, O., Zhang, J., Zhou, G., Lu, X., Liu, C., & Zhang, D. (2020). Species richness promotes ecosystem carbon storage: Evidence from biodiversity-ecosystem functioning experiments. Proceedings of the Royal Society B, 287(1939), 20202063. (https://doi.org/10.1098/rspb.2020.2063)
Tennekes M (2018). 「tmap: Thematic Maps in R.」 Journal of Statistical Software, 84(6), 1-39. doi: 10.18637/jss.v084.i06 (https://doi.org/10.18637/jss.v084.i06).