简体   繁体   English

获取R中分散数据的多个多边形

[英]Get multiple polygons for scattered data in R

I have point cloud data of an area (x,y,z coordinates) The plot of X and Y looks like:我有一个区域的点云数据(x,y,z 坐标) X 和 Y 的 plot 看起来像: 在此处输入图像描述

I am trying to get polygons of different clusters in this data.我正在尝试在此数据中获取不同集群的多边形。 I tried the following:我尝试了以下方法:

points <- df [,1:2] # x and y coordinates 
pts <- st_as_sf(points, coords=c('X','Y'))
conc <- concaveman(pts, concavity = 0.5, length_threshold = 0)

Seems like I just get a single polygon binding the whole data.好像我只是得到一个绑定整个数据的多边形。 conc$polygons is a list of one variable. conc$polygons是一个变量的列表。 How can I define multiple polygons?如何定义多个多边形? What am I missing when I am using concaveman and what all it can provide?当我使用 concaveman 时我错过了什么以及它可以提供什么?

It's hard to tell from your example what variable defines your clusters.从您的示例中很难分辨出哪个变量定义了您的集群。 Below is an example with some simulated clusters using ggplot2 and data.table (adapted from here ).下面是使用ggplot2data.table (改编自此处)的一些模拟集群的示例。

library(data.table)
library(ggplot2)

# Simulate data:
set.seed(1)
n_cluster = 50
centroids = cbind.data.frame(
  x=rnorm(5, mean = 0, sd=5),
  y=rnorm(5, mean = 0, sd=5)
)
dt = rbindlist(
  lapply(
    1:nrow(centroids),
    function(i) {
      cluster_dt = data.table(
        x = rnorm(n_cluster, mean = centroids$x[i]),
        y = rnorm(n_cluster, mean = centroids$y[i]),
        cluster = i
      )
    }
  )
)
dt[,cluster:=as.factor(cluster)]

# Find convex hull of each point by cluster:
hulls = dt[,.SD[chull(x,y)],by=.(cluster)]

# Plot:
p = ggplot(data = dt, aes(x=x, y=y, colour=cluster)) +
  geom_point() +
  geom_polygon(data = hulls,aes(fill=cluster,alpha = 0.5)) +
  guides(alpha=F)

This produces the following output:这将产生以下 output:

带有模拟集群的多边形图。

Edit编辑

If you don't have predefined clusters, you can use a clustering algorithm.如果您没有预定义的集群,则可以使用集群算法。 As a simple example, see below for a solution using kmeans with 5 centroids.作为一个简单的示例,请参见下面的使用具有 5 个质心的kmeans的解决方案。

# Estimate clusters (e.g. kmeans):
dt[,km_cluster := as.factor(kmeans(.SD,5)$cluster),.SDcols=c("x","y")]

# Find convex hull of each point:
hulls = dt[,.SD[chull(x,y)],by=.(km_cluster)]

# Plot:
p = ggplot(data = dt, aes(x=x, y=y, colour=km_cluster)) +
  geom_point() +
  geom_polygon(data = hulls,aes(fill=km_cluster,alpha = 0.5)) +
  guides(alpha=F)

In this case the output for the estimated clusters is almost equivalent to the constructed ones.在这种情况下,估计集群的 output 几乎等同于构建的集群。

从 k-means 聚类估计的多边形图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM