r - 用R中的地图边界划定voronoi图
问题描述
我有一组纬度和经度点,我设法绘制了一个 voronoi 图。
我想将 voronoi 图覆盖在新加坡的 shapefile 上,并用地图的边界划定 voronoi 图。
我拥有的 voronoi 图是:
coords <- data.frame(Lat=c(1.29370,1.37640,1.25600,1.38370,1.38240,1.31910),Long=c(103.8125,103.8492,103.6790,103.8860,103.7603,103.8191))
library(deldir)
library(ggplot2)
#This creates the voronoi line segments
voronoi <- deldir(coords$Long, coords$Lat)
#Now we can make a plot
ggplot(data=coords, aes(x=Long,y=Lat)) +
#Plot the voronoi lines
geom_segment(
aes(x = x1, y = y1, xend = x2, yend = y2),
size = 2,
data = voronoi$dirsgs,
linetype = 1,
color= "#FFB958") +
#Plot the points
geom_point(
fill=rgb(70,130,180,255,maxColorValue=255),
pch=21,
size = 4,
color="#333333")
我的地图是:
library(raster)
sg <- getData(country="SGP", level=0)
如何在地图上绘制 voronoi 图并按地图边界划分?
解决方案
您可以使用dismo::voronoi
后跟raster::intersect
library(dismo)
library(rgeos)
coords <- data.frame(Lat=c(1.29370,1.37640,1.25600,1.38370,1.38240,1.31910),Long=c(103.8125,103.8492,103.6790,103.8860,103.7603,103.8191))
sg <- getData(country="SGP", level=0)
创建 Voronoi 图
v <- voronoi(coords[, c('Long', 'Lat')], ext=extent(sg)+.1)
与多边形相交
sgv <- v * sg
# or # sgv <- instersect(v, sg)
用于绘图
plot(sgv)
或者
g <- gmap(sg, lonlat=TRUE, scale=2)
plot(g, interpolate=TRUE)
lines(sgv, col='red')
推荐阅读
- discord.js - 获取用户在频道中提及的次数
- laravel - 标记每个资源的重叠事件(FullCalendar 5,Laravel 7)
- ios - 从 `URLSession.dataTaskPublisher` 接收任务进度
- android - 是否可以在 android 设备上构建推送通知的文本?
- c++ - 如果它只是“浅常量”而不是“深常量”,我应该声明方法“常量”吗?
- reactjs - 部署生产构建时 React App 空白页面
- image - XAML 将图像加载到网格中
- node.js - 未返回 NodeJS 请求
- html - 垂直居中不适用于 flexbox
- c# - 表单中未收到从视图中发送的数据