在R中用地图边界定界voronoi图

时间:2018-06-22 02:53:40

标签: r ggplot2 gis voronoi

我有一组纬度和经度点,我设法用它绘制了一个voronoi图。

我想将voronoi图覆盖在新加坡的shapefile上,并用地图的边界定界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图并通过地图边界定界?

2 个答案:

答案 0 :(得分:2)

您可以使用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')

答案 1 :(得分:0)

也许这会给你一些想法:

library(deldir)
library(ggplot2)
library (ggmap)

coords <- data.frame(
    lat=c(1.29370, 1.37640, 1.25600, 1.38370, 1.38240, 1.31910),
    lon=c(103.8125, 103.8492, 103.6790, 103.8860, 103.7603, 103.8191)
    )

voronoi <- deldir(coords$lon, coords$lat)

singapore.map <- get_map("singapore", zoom=11)

ggmap(singapore.map, extent = "normal")+
  geom_segment(
    aes(x=x1, y=y1, xend=x2, yend=y2),
    size=2,
    data=voronoi$dirsgs,
    linetype=1,
    color= "#FFB958")+ 
  geom_point(
    fill=rgb(70, 130, 180, 255, maxColorValue=255),
    pch=21,
    size=4,
    color="#333333")