簡體   English   中英

獲取R中分散數據的多個多邊形

[英]Get multiple polygons for scattered data in R

我有一個區域的點雲數據(x,y,z 坐標) X 和 Y 的 plot 看起來像: 在此處輸入圖像描述

我正在嘗試在此數據中獲取不同集群的多邊形。 我嘗試了以下方法:

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)

好像我只是得到一個綁定整個數據的多邊形。 conc$polygons是一個變量的列表。 如何定義多個多邊形? 當我使用 concaveman 時我錯過了什么以及它可以提供什么?

從您的示例中很難分辨出哪個變量定義了您的集群。 下面是使用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)

這將產生以下 output:

帶有模擬集群的多邊形圖。

編輯

如果您沒有預定義的集群,則可以使用集群算法。 作為一個簡單的示例,請參見下面的使用具有 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)

在這種情況下,估計集群的 output 幾乎等同於構建的集群。

從 k-means 聚類估計的多邊形圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM