【发布时间】:2021-04-03 21:47:47
【问题描述】:
我希望为多个多边形创建一个遮罩多边形。
单个多边形很容易:
How to apply a polygon mask layer in ggplot
但对于多个来说要复杂得多:
https://www.stat.auckland.ac.nz/~paul/Reports/GraphicsEngine/definitions/definitions.html
我觉得我已经很接近了,但我需要一种在最近多边形的点之间画一条线的方法,即我想尝试用一条线连接最近的岛屿。
同时将这些多边形连接到外部多边形框,但仅使用一条线:
library(tidyverse)
library(sf)
library(albersusa)
usa <- usa_sf()
HI <- st_coordinates(usa %>%
filter(name %in% c("Hawaii"))) %>%
as.data.frame() %>%
select(X, Y)
rec_box <-
data.frame(
X = c(-108,-108,-101,-101,-108),
Y = c(24, 28, 28, 24, 24)
)
mask <- rbind(HI, rec_box)
eg <- st_as_sf(data.frame(mask), coords = c("X", "Y"))
poly <- st_convex_hull(eg)
ggplot() +
geom_sf(data = poly) +
geom_density2d_filled(data = HI, aes(x = X, y = Y)) +
geom_polygon(data = mask,
aes(x = X, y = Y),
color = "black",
fill = "white")
【问题讨论】: