r - 在边界框的边缘剪裁的 Voronoi 多边形

标签 r ggplot2 polygon ggmap voronoi

如果这篇文章的标题让您感到困惑,我很抱歉,我想不出一个更准确的标题。我正在尝试以一定的缩放级别在休斯顿城市 map 上叠加使用休斯顿犯罪数据生成的 Voronoi 多边形。当我自己绘制多边形并使用 coord_cartesian(xlim =, ylim =) 将可见区域限制在 map 的边界框内时,绘图看起来不错(请参见下面的第一个绘图)。但是当它们被绘制在城市 map 上时,盒子边缘附近的多边形被剪裁和扭曲(第二个图),据我所知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 谢谢。

最佳答案

可以通过向 ggmap 添加参数 extent = "normal"maprange = FALSE 来解决这个问题:

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: logical for use with base_layer; should the map define the x and y limits?

extent: how much of the plot should the map take up? "normal", "device", or "panel" (default)

expand = 0 添加到 coord_cartesian 结果:

enter image description here

关于r - 在边界框的边缘剪裁的 Voronoi 多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48733640/

相关文章:

android - 给定 map 叠加层上的一组点绘制空多边形 (Android 2.1)

icons - KML:有带图标或图钉的多边形吗?

python - R saving Naive Bayes for training,R 相当于 Pythons pickle。

r - 绘制带有正方形和线条的图的放大插图图

r - 将 geom_jitter 中的点保持在零以上

r - 在条形图中使用 position_dodge(preserve ="single") 时,geom_text 未正确定位

c# - 线段的多边形

mysql - 基于列合并两个表(部分匹配或逗号分隔列)?

R Shiny showModal 和removeModal 适用于所有用户

给定条件替换 data.table 中的所有值