我正在尝试删除 Voronoi 图中的外部多边形。主线剧情是这样的:
require(ggplot2)
require(ggvoronoi)
set.seed(2023)
N <- 80
x <- runif(N)
y <- runif(N)
df <- data.frame(x, y)
df$dist <- rnorm(N)
ggplot(df, aes(x, y, fill = dist)) +
geom_voronoi() +
stat_voronoi(geom = "path") +
theme_void()
如果我在轴上设置限制,就会发生接近我想要的事情。然而,仍然填充了一些外部多边形:
ggplot(df, aes(x, y, fill = dist)) +
geom_voronoi() +
stat_voronoi(geom = "path") +
theme_void() +
xlim(0.1, 0.9) +
ylim(0.1, 0.9)
[
隔离/识别图形边缘周围最外层的多边形并使它们透明或统一颜色的最佳方法是什么?
不幸的是我无法安装
ggvoronoi
。但这里有一种使用 ggforce::geom_tile_voronoi
的方法,也许也适用于 ggvoronoi
。这种方法有点老套,因为我只是从 geom_tile_voronoi
层中提取数据,然后过滤接触边界的多边形,然后使用 geom_polygon
绘制过滤后的数据,而无需外部多边形:
library(ggplot2)
library(ggforce)
library(dplyr, warn=FALSE)
set.seed(2023)
N <- 80
x <- runif(N)
y <- runif(N)
df <- data.frame(x, y)
df$dist <- rnorm(N)
p <- ggplot(df, aes(x, y, fill = dist)) +
geom_voronoi_tile() +
theme_void()
dat <- layer_data(p, i = 1)
outer <- dat |>
filter(x %in% range(x) | y %in% range(y)) |>
distinct(group) |>
pull(group)
dat |>
filter(!group %in% outer) |>
ggplot(aes(x, y, fill = fill)) +
geom_voronoi_tile(
data = df,
aes(x, y),
fill = "white", color = "black"
) +
geom_polygon(aes(group = group), color = "black") +
scale_fill_identity() +
theme_void()
dat |>
filter(!group %in% outer) |>
ggplot(aes(x, y, fill = fill)) +
geom_polygon(aes(group = group), color = "black") +
scale_fill_identity() +
theme_void()