ggplot忽略了多边形内部的空洞。

6

我在使用ggplot绘制带空洞多边形时遇到了困难。以下是演示过程。首先使用git clone https://github.com/geotheory/volcano获取shapefile文件。接下来:

require(ggplot2); require(ggmap); require(dplyr); require(maptools)
v = readShapePoly('volcano/volcano.shp')
v@proj4string = CRS('+proj=longlat +datum=WGS84')

# confirm polygons spatially exclusive (don't overlap)
plot(t(bbox(v)), type='l', lwd=8)
plot(v, col=paste0(colorRampPalette(c('grey','red'))(8),'dd'), add=T)

enter image description here

看起来还不错。如果被多个多边形遮挡,ddalpha 应该将线条渲染为不可见。现在让我们在 ggplot 中尝试。

d = fortify(v) %>% as_data_frame()
bb = bbox(v)
toner = get_stamenmap(c(bb[1,1], bb[2,1], bb[1,2], bb[2,2]), zoom=11, maptype='toner')
ggmap(toner) + geom_polygon(data=d, aes(long, lat, group=group, fill=id), alpha=.5)

图片描述在此输入

由于中心区域完全被覆盖,因此中央多边形必须重叠。让我们检查加固数据是否有漏洞:

d %>% select(id, hole) %>% table()
   hole
id  FALSE TRUE
  0   278    0
  1   715    0
  2   392  388
  3   388  331
  4   390  265
  5   265  387
  6   328  125
  7   125    0

看起来不错,那么让我们尝试分别对它们进行可视化。

i = 3
plot(v[i,], col='red') 
ggplot(filter(d, id == i-1)) + geom_polygon(aes(long, lat, group=group, col=hole), fill=NA)
ggplot() + geom_polygon(data=filter(d, id==i-1), aes(long,lat, group=group))

这里输入图像描述

似乎出了些问题,ggplot似乎忽略了空洞。除非是形状文件有问题。有什么建议可以诊断/修复此问题吗?

2个回答

4

"hadley/ggplot2: plotting polygon shapefiles" 表示 "..., 保留了空洞状态,但是ggplot2没有使用这些信息"。幸运的是,ggspatial 包, "Fish & Whistle: Holes in ggplot polygons" 教我一些解决方案,例如重叠问题。(白色区域在标签“Biggin Hill”上方并不是因为 ggspatial 包造成的,因为在我的环境中也出现在 ggplot(d) + geom_polygon(aes(long, lat, group = group, fill = id))

devtools::install_github("paleolimbot/ggspatial")

library(ggspatial)

ggmap(toner) + geom_spatial(data=v, aes(fill=id), alpha=.8)

enter image description here


1
我在世界地图上遇到了与莱索托国相同的问题。通常,ggplot2应该检测到洞,因为多边形的方向是反向的(如果我没记错的话,是逆时针而不是顺时针)。这里的解决方案似乎是从选择中删除孔。
colorsbrewer_red <-  c( "#fff5f0","#fee0d2", "#fcbba1","#fc9272", "#fb6a4a", "#ef3b2c", "#cb181d",  "#99000d")

ggmap(toner) +  
geom_polygon(data=d[!d$hole,], aes(long, lat, group=group, fill=id), alpha=.5) +
  scale_fill_manual(name = "",
                    values= colorsbrewer_red,
                    na.value = "# 808080",
                    drop=FALSE)

volcano_in_london

无论如何,如果有人知道如何直接处理geom_polygon中的孔洞,那就太好了。

谢谢 @timat。对我来说还是个问题,因为我需要这些空洞,否则多边形会重叠,而底层地图则会被遮挡住。 - geotheory

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接