簡體   English   中英

如何繪制Voronoi鑲嵌的多邊形而不是線段?

[英]How can I draw the polygons of a Voronoi Tesselation instead of the segments?

我找到了一種使用ggplot2繪制Voronoi鑲嵌細分的方法:

library(deldir)
library(ggplot2)
library(ggthemes)
set.seed(123)
df <- data.frame(lat = rnorm(20,39,10),long = rnorm(20,-98,15))
voronoi <- deldir(df$long, df$lat)

ggplot(data=df, aes(x=long,y=lat)) +
  geom_segment(aes(x = x1, y = y1, xend = x2, yend = y2),size = 2,data = voronoi$dirsgs,linetype = 1,color= "#419AB0") +
  geom_point(fill="#EACA3E",pch=21,size = 4,color="white") 

我想知道是否可以在線段之間繪制多邊形,但是我不知道如何使用每個多邊形的輪廓創建數據集。

這是將線段轉換為SpatialPolygons對象的簡單方法。

library(rgeos)

## Convert data.frame of segment coordinates to a list of SpatialLines objects
ll <- apply(voronoi$dirsgs, 1, FUN=function(X) {
    readWKT(sprintf("LINESTRING(%s %s, %s %s)", X[1], X[2], X[3], X[4]))
})

## Convert SpatialLines list to SpatialPolygons object
pp <- gPolygonize(ll)

## Plot to check that it works    
set.seed=11
plot(pp, col=sample(colors(), length(pp)))

在此處輸入圖片說明

暫無
暫無

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

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