How calculate the area of a polygon in R, if I only have X,Y coordinates?

江枫思渺然 提交于 2021-02-11 12:17:22

问题


I have a data frame with X, Y coordinates, but I need to calculate the area that cover all the points in the scatterplot there is a way to draw a polygon that surround all the points and calculate this area?


回答1:


This is a convex hull problem. Other questions cover similar territory. We can gather the plotting and area calculation problems here for good measure.

To plot the convex hull of a point cloud, we can use the chull function:

library(tidyverse)

data <- tibble(x = runif(50), y = runif(50))

convex_hull <- data %>% slice(chull(x, y))

ggplot(data, aes(x = x, y = y)) + 
  geom_point() + 
  geom_polygon(data = convex_hull,
               alpha = 0.25)

Library splancs has an areapl function to calculate the area of a non-selfintersecting polygon, such as the convex_hull above. Hence, we may do:

library(splancs)
as.matrix(convex_hull) %>% areapl


来源:https://stackoverflow.com/questions/58772665/how-calculate-the-area-of-a-polygon-in-r-if-i-only-have-x-y-coordinates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!