一种方法使用sf 和dplyr 进行“地理计算”和过滤,使用tmap 和tmaptools 进行可视化。
请找到以下表示。
Reprex
library(sf)
library(dplyr)
library(tmap)
library(tmaptools)
sf_use_s2(TRUE)
dat_sf <- st_as_sf(dat, coords = c("lon", "lat"), crs = 4326)
conv_nor <- st_simplify(st_buffer(st_convex_hull(st_union(st_geometry(filter(dat_sf, region == "nor")))), dist = 15000), dTolerance = 5000)
conv_us <- st_simplify(st_buffer(st_convex_hull(st_union(st_geometry(filter(dat_sf, region == "us")))), dist = 15000), dTolerance = 5000)
conv_ak <- st_simplify(st_buffer(st_convex_hull(st_union(st_geometry(filter(dat_sf, region == "ak")))), dist = 15000), dTolerance = 5000)
tm_shape(dat_sf)+
tm_dots(size = 0.2, col = "black", shape = 21, alpha = 0.3)+
tm_shape(conv_nor)+
tm_borders(col = "gray", lwd = 2)+
tm_shape(conv_us)+
tm_borders(col = "blue", lwd = 2)+
tm_shape(conv_ak)+
tm_borders(col = "gray", lwd = 2)+
tm_layout(frame = FALSE)
tm_shape(filter(dat_sf, region == "nor"), bbox = bb(conv_nor, ext = 1))+
tm_dots(size = 0.5, alpha = 0.5)+
tm_shape(conv_nor)+
tm_borders(col = "gray", lwd = 2)+
tm_layout(frame = FALSE, title = "'nor' area",
title.size = 1.5, title.position = c(0.85, "bottom"))
tm_shape(filter(dat_sf, region == "us"), bbox = bb(conv_us, ext = 1))+
tm_dots(size = 0.5, alpha = 0.5)+
tm_shape(conv_us)+
tm_borders(col = "gray", lwd = 2)+
tm_layout(frame = FALSE, title = "'us' area",
title.size = 1.5, title.position = c(0., "top"))
tm_shape(filter(dat_sf, region == "ak"), bbox = bb(conv_ak, ext = 1))+
tm_dots(size = 0.5, alpha = 0.5)+
tm_shape(conv_ak)+
tm_borders(col = "gray", lwd = 2)+
tm_layout(frame = FALSE, title = "'ak' area",
title.size = 1.5, title.position = c(0.75, "top"))
最后,如果你想要一些上下文背景,你可以使用tmap_mode("view") 调用leaflet - 显然你在海洋环境中工作;-)
tmap_mode("view")
#> tmap mode set to interactive viewing
tm_shape(filter(dat_sf, region == "nor"), bbox = bb(conv_nor, ext = 1))+
tm_dots(size = 0.5, alpha = 0.5)+
tm_shape(conv_nor)+
tm_borders(col = "gray", lwd = 2)+
tm_layout(frame = FALSE, title = "'nor' area",
title.size = 1.5, title.position = c(0.85, "bottom"))
tm_shape(filter(dat_sf, region == "us"), bbox = bb(conv_us, ext = 1))+
tm_dots(size = 0.5, alpha = 0.5)+
tm_shape(conv_us)+
tm_borders(col = "gray", lwd = 2)+
tm_layout(frame = FALSE, title = "'us' area",
title.size = 1.5, title.position = c(0., "top"))
tm_shape(filter(dat_sf, region == "ak"), bbox = bb(conv_ak, ext = 1))+
tm_dots(size = 0.5, alpha = 0.5)+
tm_shape(conv_ak)+
tm_borders(col = "gray", lwd = 2)+
tm_layout(frame = FALSE, title = "'ak' area",
title.size = 1.5, title.position = c(0.75, "top"))
tmap_mode("plot")
#> tmap mode set to plotting
由reprex package (v2.0.1) 于 2021-10-20 创建