I'm trying to remove the outer polygons in a Voronoi diagram. The main plot looks like this:
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()
Something that's close to what I would like occurs if I set a limit on the axes. However, there's still fill in some of the outer polygons:
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)
[
What is the best approach to isolate/identify the outer-most polygons around the edge of the figure and make them transparent or a uniform colour?



{sf}-based approach (GEOS implementation ofst_voronoi) might look something like this:Added
st_crop(points_sf)to deal with a larger-than-optimal envelope size ofst_voronoi(); this should probably be reconsidered depending on the actual dataset and application.First revision - https://stackoverflow.com/revisions/77569621/1