Reputation: 31
I'd like to extract the areas each observation from ggvoronoi such that I get a new dataframe with the first column being the observation and the next column being the area of the polygon.
library(ggvoronoi)
set.seed(45056)
x <- sample(1:200,100)
y <- sample(1:200,100)
ggplot(points,aes(x,y)) +
stat_voronoi(geom=“path”) +
geom_point()
Upvotes: 3
Views: 313
Reputation: 61
ggvoronoi uses the deldir library to compute Voronoi tessellation. deldir returns a list of objects, which has a summary data frame for the points triangulated and the areas you are looking for.
You could run deldir on the same input to get the areas - dir.area
column in the summary. For example:
?deldir
x = c(0, 1)
y = c(0, 1
d = deldir(x, y)
> d$summary
x y n.tri del.area del.wts n.tside nbpt dir.area dir.wts
1 0 0 0 0 NaN 1 2 0.72 0.5
2 1 1 0 0 NaN 1 2 0.72 0.5
Upvotes: 0
Reputation: 1780
First convert your data to a sf object:
library(sf)
set.seed(45056)
x <- sample(1:200,100)
y <- sample(1:200,100)
points <- data.frame(x = x, y = y)
points_sf <- st_as_sf(points, coords = c("x", "y"))
Then you can use the following functions to calculate the area of each polygon:
v <- points_sf %>%
st_union() %>%
st_voronoi() %>%
st_collection_extract() %>%
st_area()
Combine with the original data:
cbind(x, y, v)
Upvotes: 1