围绕R中的坐标绘制多边形
Draw polygons around coordinates in R
我有来自不同地理区域收集的不同数据集的点簇。我想在地图上绘制每个区域的空间范围,为此我需要每个区域一个多边形。我可以找到通过“连接点”将点转换为多边形的命令,但是 none 通过绘制轮廓(几何方法不是很重要,因为这只是为了绘图)。这是一个生成简化数据框的代表,就像我正在使用的那样,用于挪威、阿拉斯加和美国大陆的点:
nor_lat <- c(72.117, 71.05, 71.717, 71.167, 71.817, 72.333, 71.25, 70.917,
70.85, 70.933)
nor_lon <- c(33.217, 30.333, 26.3, 22.333, 22.333, 22.333, 18.517, 20.133,
21.333, 19.917)
us_lat <- c(41.283333, 41, 40.95, 40.95, 38.683333, 40.783333, 40.733333,
40.516667, 38.566667, 41.266667)
us_lon <- c(-71.116667, -70.733333, -70.583333, -70.483333, -74.816667,
-70.5, -70.566667, -70.266667, -74.85, -71.366667)
ak_lat <- c(58.015, 57.67167, 57.33833, 56.685, 56.99333, 57.31667, 57.65,
57.99667, 58.32167, 58.33333)
ak_lon <- c(-158.31667, -158.36, -158.41, -159.76, -159.72333, -159.66333,
-159.64167, -159.605, -159.54, -160.72167)
nor_dat <- data.frame(lat=nor_lat, lon=nor_lon)
us_dat <- data.frame(lat=us_lat, lon=us_lon)
ak_dat <- data.frame(lat=ak_lat, lon=ak_lon)
nor_dat$region <- "nor"
us_dat$region <- "us"
ak_dat$region <- "ak"
dat <- rbind(nor_dat, us_dat, ak_dat)
对于对象 dat
,我想在每个 region
中的点周围生成一个简单的多边形,如果可能的话使用 sf。 (我不太担心如何遍历区域,但我什至不知道如何围绕一组点绘制多边形!)
一种使用 sf
和 dplyr
进行“地理计算”和过滤,tmap
和 tmaptools
进行可视化的方法。
请找到以下代表。
Reprex
- 将
dataframe
“dat”转换为 sf
对象
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"))
- 带背景的详细视图
最后,如果您想要一些上下文背景,可以使用调用 leaflet
的 tmap_mode("view")
- 显然您在海洋环境中工作 ;-)
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 创建
library(sf)
library(dplyr)
hulls <- dat %>%
st_as_sf(coords = c("lon", "lat")) %>%
group_by(region) %>%
summarize(geometry = st_union(geometry)) %>%
st_convex_hull()
plot(hulls)
我有来自不同地理区域收集的不同数据集的点簇。我想在地图上绘制每个区域的空间范围,为此我需要每个区域一个多边形。我可以找到通过“连接点”将点转换为多边形的命令,但是 none 通过绘制轮廓(几何方法不是很重要,因为这只是为了绘图)。这是一个生成简化数据框的代表,就像我正在使用的那样,用于挪威、阿拉斯加和美国大陆的点:
nor_lat <- c(72.117, 71.05, 71.717, 71.167, 71.817, 72.333, 71.25, 70.917,
70.85, 70.933)
nor_lon <- c(33.217, 30.333, 26.3, 22.333, 22.333, 22.333, 18.517, 20.133,
21.333, 19.917)
us_lat <- c(41.283333, 41, 40.95, 40.95, 38.683333, 40.783333, 40.733333,
40.516667, 38.566667, 41.266667)
us_lon <- c(-71.116667, -70.733333, -70.583333, -70.483333, -74.816667,
-70.5, -70.566667, -70.266667, -74.85, -71.366667)
ak_lat <- c(58.015, 57.67167, 57.33833, 56.685, 56.99333, 57.31667, 57.65,
57.99667, 58.32167, 58.33333)
ak_lon <- c(-158.31667, -158.36, -158.41, -159.76, -159.72333, -159.66333,
-159.64167, -159.605, -159.54, -160.72167)
nor_dat <- data.frame(lat=nor_lat, lon=nor_lon)
us_dat <- data.frame(lat=us_lat, lon=us_lon)
ak_dat <- data.frame(lat=ak_lat, lon=ak_lon)
nor_dat$region <- "nor"
us_dat$region <- "us"
ak_dat$region <- "ak"
dat <- rbind(nor_dat, us_dat, ak_dat)
对于对象 dat
,我想在每个 region
中的点周围生成一个简单的多边形,如果可能的话使用 sf。 (我不太担心如何遍历区域,但我什至不知道如何围绕一组点绘制多边形!)
一种使用 sf
和 dplyr
进行“地理计算”和过滤,tmap
和 tmaptools
进行可视化的方法。
请找到以下代表。
Reprex
- 将
dataframe
“dat”转换为sf
对象
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"))
- 带背景的详细视图
最后,如果您想要一些上下文背景,可以使用调用 leaflet
的 tmap_mode("view")
- 显然您在海洋环境中工作 ;-)
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 创建library(sf)
library(dplyr)
hulls <- dat %>%
st_as_sf(coords = c("lon", "lat")) %>%
group_by(region) %>%
summarize(geometry = st_union(geometry)) %>%
st_convex_hull()
plot(hulls)