本文介绍了我如何绘制Voronoi Tesselation的多边形而不是段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 library(deldir)$ b我已经找到了一种使用ggplot2绘制Voronoi镶嵌细分的方法: $ b library(ggplot2) library(ggthemes) set.seed(123) df< - data.frame(lat = rnorm(20,39,10),long = rnorm (20,-98,15)) voronoi 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) ##将段坐标的data.frame转换为列表SpatialLines对象 ll< - apply(voronoi $ dirsgs,1,FUN = function(X){ readWKT(sprintf(LINESTRING(%s%s,%s%s),X [ 1],X [2],X [3],X [4]))}) ##将SpatialLines列表转换为SpatialPolygons对象 pp ##绘图来检查它是否工作 set.seed = 11 plot(pp,col = sample(colors(),length(pp))) I've found a method to draw the segments of a Voronoi tesselation using ggplot2 : 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") I would like to know if it is possible to draw polygons intead of segments but I don't know how to create a dataset with the contour of each polygon. 解决方案 Here's a simple way to convert the line segments to SpatialPolygons objects.library(rgeos)## Convert data.frame of segment coordinates to a list of SpatialLines objectsll <- 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 objectpp <- gPolygonize(ll)## Plot to check that it works set.seed=11plot(pp, col=sample(colors(), length(pp))) 这篇关于我如何绘制Voronoi Tesselation的多边形而不是段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 02:58