10

我想SpatialPolygonsspggplot2 中有孔的库中绘图。感谢其他关于stackoverflow的问题,我知道在处理顺时针书写的多边形时这是允许的:
http
://stackoverflow.com/questions/12047643/geom-polygon-with-multiple-hole/12051278#12051278 事实上,当转换一个SpatialPolygons使用broom::tidy(替换ggplot2::fortify),孔多边形按顺时针方向保存以绘制为孔。
在 ggplot2 中,绘制带孔的多边形的方式强制使用 绘制它们一次fill,然后使用 绘制它们colour,否则您可能会看到线穿过多边形。当处理多个子多边形时,有些带有孔,这更棘手,点特征的顺序定义为broom::tidy可能不允许填充多边形(见下图)。
你们中的任何人都有摆脱这种填充问题行为的解决方案吗?

这是一个可重现的示例:

library(sp)
library(ggplot2)

# Create two polygons: second would be a hole inside the first
xy = cbind(
  x = c(13.4, 13.4, 13.6, 13.6, 13.4),
  y = c(48.9, 49, 49, 48.9, 48.9)
    )
hole.xy <- cbind(
  x = c(13.5, 13.5, 13.45, 13.45, 13.5),
  y = c(48.98, 48.92, 48.92, 48.98, 48.98)
  )

# Transform as SpatialPolygons with holes
xy.sp <- SpatialPolygons(list(
  Polygons(list(Polygon(xy),
                Polygon(hole.xy, hole = TRUE)), "1"),
  Polygons(list(Polygon(xy + 0.2),
                Polygon(xy + 0.35),
                Polygon(hole.xy + 0.2, hole = TRUE)), "2")
  ))

# Transform SpatialObject to be used by ggplot2
xy.sp.l <- broom::tidy(xy.sp)

ggplot(xy.sp.l) +
  geom_polygon(aes(x = long, y = lat, group = id, fill = id))

带有孔的SpatialPolygons的ggplot填充问题
(来源:statnmap.com

4

3 回答 3

11

可能是“翻阅”sf包裹的好时机。由于几何,使用sf对象实际上要容易得多:ggplotgeom_sf

library("sf")
library("rgeos")
sf_poly <- as(xy.sp, "sf")
sf::st_crs(sf_poly) <- 4326
sf_poly$id <- c(1,2)
ggplot(sf_poly) +
  geom_sf(aes(fill = as.factor(id)))

在此处输入图像描述

于 2017-05-29T19:19:04.150 回答
1

这篇文章是一个很好的问题,并且已经收到了很好的答案。我也相信人们应该学习如何使用sf对象,因为它是 R 中的下一代空间数据类型。但我想分享一下,在这种情况下geom_spatial,从ggspatial包中可以选择绘制 SpatialPolygons。

library(sp)
library(ggplot2)
library(ggspatial)

ggplot() +
  geom_spatial(xy.sp, aes(fill = id))
# Ignoring argument 'mapping' in geom_spatial.SpatialPolygons
# Autodetect projection: assuming lat/lon (epsg 4326)

在此处输入图像描述

于 2017-12-06T17:40:13.210 回答
1

添加行显示了问题的根源。正在绘制蓝色“多边形”,下 -> 上 -> 孔。

在此处输入图像描述

这段代码(不是很优雅,抱歉)使路径回到第一个片段的起点,然后再进入第三个片段。

 library(dplyr)
    extra <- xy.sp.l %>%
        filter(piece != 1) %>%
        group_by(id, group) %>%
        summarise(last_pt = max(order))


for (n in 1:nrow(extra)) {
    id_ex <- as.character(extra[n,"id"])
    x <- subset(xy.sp.l, id == id_ex & piece == 1 & order == 1)
    x$order <- as.numeric(extra[n,"last_pt"]) + 0.5
    xy.sp.l <- rbind(xy.sp.l,x)
}

xy.sp.l <- xy.sp.l[order(xy.sp.l$id, xy.sp.l$order),] 

在此处输入图像描述

于 2017-05-29T19:33:55.107 回答