基于shp格式的底图,R包sf进行矢量空间数据的转换,R包tmap进行可视化
加载工具包
library(sf)
library(tmap)
library(ggplot2)
library(tmaptools)
sf_use_s2(FALSE)
library(scales)
设置颜色变量
# forest, grass, shrub
arid = c('#8E0152', '#C51B7D','#DE77AE','#F1B6DA')
tropical = c('#40004B','#762A83','#9970AB','#C2A5CF')
temperate = c("#003C30",'#01665E','#35978F','#80CDC1')
continental = c("#543005",'#8C510A','#BF812D','#DFC27D')
polar = '#FFFF33'
show_col(c(arid, tropical, temperate, continental, polar))
show_col(grep('^grey', colors(), value = T))
设置R语言中的地理/投影坐标系统
proj = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
导入样点数据
包括坐标信息和分组信息
sites <- read.table("./sites.csv", sep = ",", header = T)
names(sites)[4] = 'Biomes'
sites.sf <- st_as_sf(sites, coords = c("lon", "lat"), crs=4326)#25831
sites.sf.robin <- st_transform(sites.sf, proj)
导入底图
# land = read_sf('10m_physical/ne_10m_land.shp') %>%
# # subset(CONTINENT != 'Antarctica') %>%
# st_geometry() %>%
# st_transform(proj)
country = read_sf('10m_cultural/ne_10m_admin_0_countries.shp')%>%
# subset(name != 'Antarctica') %>%
st_geometry() %>%
st_transform(proj)
绘图
fig = tm_shape(country)+
tm_graticules(col = 'grey80',labels.show = F) +
tm_borders(col = 'white', lwd = .01) +
tm_fill(col = 'grey',border.col = 'black') +
tm_layout(bg.color="white",
outer.margins = c(0.001,0.001,0.001,0.1),
inner.margins=c(.02, .02, .04, .02), #bottom,
earth.boundary = T,
earth.boundary.color = 'grey80',
space.color="grey90",
legend.show = T,
outer.bg.color = 'white')