Sorry if the title of this post confuses you, I could not come up with a more precise one. I am trying to overlay Voronoi polygons, generated using Houston crime data, on Houston city map at a certain zoom level. When I plot the polygons by themselves and restrict the visible area to the map's bounding box usingcoord_cartesian(xlim =, ylim =),
the plot looks fine (see 1st plot below). But when they are plotted over the city map, polygons near the box edges are clipped and distorted (2nd plot), which as I understand from this cheatsheet should not happen. What am I doing wrong?
对不起,如果这篇文章的标题让你困惑,我想不出一个更精确的。我正在尝试用休斯顿犯罪数据,在休斯顿城市地图上,在一定的缩放级别上覆盖Voronoi多边形。当我单独绘制多边形并使用coord_cartesian(xlim =, ylim =)将可见区域限制在地图的边界框中时,这个图看起来很好(见下面第一个图)。但是当它们被绘制在城市地图上时,盒子边缘附近的多边形会被剪切和扭曲(第二幅图),我从这张欺骗图上知道这是不应该发生的。我做错了什么?
# 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))
谢谢。
1 个解决方案
#1
3
The issue can be solved by adding the arguments extent = "normal"
and maprange = FALSE
to ggmap
:
可以通过在ggmap中添加参数区段=“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())
maprange: logical for use with base_layer; should the map define the x and y limits?
maprange:用于base_layer的逻辑;地图应该定义x和y的极限吗?
extent: how much of the plot should the map take up? "normal", "device", or "panel" (default)
范围:地图应该占多大比例?“正常”、“设备”或“面板”(默认)
adding expand = 0
to coord_cartesian
results in:
将expand = 0添加到coord_cartesian结果如下:
#1
3
The issue can be solved by adding the arguments extent = "normal"
and maprange = FALSE
to ggmap
:
可以通过在ggmap中添加参数区段=“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())
maprange: logical for use with base_layer; should the map define the x and y limits?
maprange:用于base_layer的逻辑;地图应该定义x和y的极限吗?
extent: how much of the plot should the map take up? "normal", "device", or "panel" (default)
范围:地图应该占多大比例?“正常”、“设备”或“面板”(默认)
adding expand = 0
to coord_cartesian
results in:
将expand = 0添加到coord_cartesian结果如下: