Voronoi多边形裁剪在边界框

时间:2018-02-11 16:23:10

标签: r ggplot2 polygon ggmap voronoi

很抱歉,如果这篇文章的标题让您感到困惑,我无法想出更准确的帖子。我试图在休斯顿城市地图上以某个缩放级别叠加使用休斯顿犯罪数据生成的Voronoi多边形。当我自己绘制多边形并使用coord_cartesian(xlim =, ylim =),将可见区域限制到地图的边界框时,绘图看起来很好(参见下面的第1个图)。但是当它们被绘制在城市地图上时,框边缘附近的多边形被剪裁和扭曲(第二个图),据我所知from this cheatsheet不应该发生。我做错了什么?

# load Houston crime data
suppressMessages(library(ggmap))
data(crime)
set.seed(42)
crime <- crime[sample(1:nrow(crime), 2000), ] # truncated for fast run

# convert to SpatialPointsDataFrame
suppressMessages(library(sp))
coords <- SpatialPoints(crime[, c("lon", "lat")])
crime_spdf <- SpatialPointsDataFrame(coords, crime)
proj4string(crime_spdf) <- CRS("+proj=longlat +ellps=WGS84")

# create Voronoi polygons
suppressMessages(library(spatstat))
suppressMessages(library(maptools))
vor_pp <- as(dirichlet(as.ppp(crime_spdf)), "SpatialPolygons")
proj4string(vor_pp) <- CRS("+proj=longlat +ellps=WGS84")

# get Houston map
houston_map <- get_map(location = geocode("Houston"),
                       zoom = 16,
                       maptype = "satellite")

# get map bounding box
xlim <- bb2bbox(attr(houston_map, "bb"))[c(1, 3)]
ylim <- bb2bbox(attr(houston_map, "bb"))[c(2, 4)]

# create Voronoi polygon plot
vor_df <- fortify(vor_pp)
plt1 <- ggplot(data = vor_df) +
  geom_polygon(aes(x = long, y = lat, group = group),
               color = "black",
               fill = NA) +
  coord_cartesian(xlim = xlim, ylim = ylim) +
  ggtitle("Voronoi Polygon Plot") +
  theme(aspect.ratio = 1,
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

# create map + polygon plot
plt2 <- ggmap(houston_map) +
  geom_polygon(data = vor_df,
               aes(x = long, y = lat, group = group),
               color = "gray80",
               fill = "red",
               alpha = 0.2) +
  coord_cartesian(xlim = xlim, ylim = ylim) +
  ggtitle("Map + Polygon Plot") +
  theme(aspect.ratio = 1,
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

# plot side-by-side
suppressMessages(library(grid))
pushViewport(viewport(layout = grid.layout(1,2)))
print(plt1, vp=viewport(layout.pos.col = 1, layout.pos.row = 1))
print(plt2, vp=viewport(layout.pos.col = 2, layout.pos.row = 1))

enter image description here 感谢。

1 个答案:

答案 0 :(得分:3)

可以通过将参数extent = "normal"maprange = FALSE添加到ggmap来解决此问题:

ggmap(houston_map, extent = "normal", maprange = FALSE) +
  geom_polygon(data = vor_df,
               aes(x = long, y = lat, group = group),
               color = "gray80",
               fill = "red",
               alpha = 0.2) +
  coord_cartesian(xlim = xlim, ylim = ylim) +
  ggtitle("Map + Polygon Plot") +
  theme(aspect.ratio = 1,
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank())

enter image description here

  

maprange:逻辑用于base_layer;地图应该定义x   和y限制?

     

范围:地图应该占用多少情节? &#34;正常&#34 ;,   &#34;设备&#34;,或&#34;面板&#34; (默认)

expand = 0添加到coord_cartesian会导致:

enter image description here