gpt4 book ai didi

r - 从 ggvoronoi 中提取多边形的面积

转载 作者:行者123 更新时间:2023-12-02 14:07:27 25 4
gpt4 key购买 nike

我想从 ggvoronoi 中提取每个观察值的区域,以便获得一个新的数据框,其中第一列是观察值,下一列是多边形的区域。

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()

最佳答案

首先将数据转换为 sf 对象:

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"))

然后您可以使用以下函数来计算每个多边形的面积:

v <- points_sf %>% 
st_union() %>%
st_voronoi() %>%
st_collection_extract() %>%
st_area()

与原始数据结合:

cbind(x, y, v)

关于r - 从 ggvoronoi 中提取多边形的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58555407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com