假设我要绘制和标记多边形(数据框中的每一行一个多边形),我可以执行以下操作:
library(sf)
library(ggplot2)
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
nc_3857 <- sf::st_transform(nc, 3857)
ggplot(nc_3857[1:3, ]) +
geom_sf(aes(fill = AREA)) +
geom_sf_label(aes(label = NAME))
这里的NAME
仅指geometry
变量中工作正常的一个多边形点列表。
但是,如果我的geometry
变量代表每行包含多个多边形的列表,我将如何标记它们。我想要一个通用版本,可以解释不同长度的列表。例如,查看此df:
df <- structure(list(id= structure(1:2, .Label = c("A1", "A2"
), class = "factor"), geometry = structure(list(structure(list(
list(structure(c(0, 1, 3, 2, 1, 0, 0, 0, 2, 4, 4, 0), .Dim = c(6L,
2L)), structure(c(1, 1, 2, 1, 1, 2, 2, 1), .Dim = c(4L, 2L
))), list(structure(c(3, 4, 4, 3, 3, 0, 0, 1, 1, 0), .Dim = c(5L,
2L)), structure(c(3.3, 3.3, 3.8, 3.8, 3.3, 0.3, 0.8, 0.8,
0.3, 0.3), .Dim = c(5L, 2L))), list(structure(c(3, 4, 4,
3, 3, 2, 3, 3), .Dim = c(4L, 2L)))), class = c("XY", "MULTIPOLYGON",
"sfg")), structure(list(list(structure(c(0, 1, 3, 2, 1, 0, 0,
0, 2, 4, 4, 0), .Dim = c(6L, 2L)), structure(c(1, 1, 2, 1, 1,
2, 2, 1), .Dim = c(4L, 2L))), list(structure(c(3, 4, 4, 3, 3,
0, 0, 1, 1, 0), .Dim = c(5L, 2L)), structure(c(3.3, 3.3, 3.8,
3.8, 3.3, 0.3, 0.8, 0.8, 0.3, 0.3), .Dim = c(5L, 2L)))), class = c("XY",
"MULTIPOLYGON", "sfg"))), crs = structure(list(epsg = NA_integer_,
proj4string = NA_character_), class = "crs"), n_empty = 0L, precision = 0, bbox = structure(c(xmin = 0,
ymin = 0, xmax = 4, ymax = 4), class = "bbox"), class = c("sfc_MULTIPOLYGON",
"sfc"))), row.names = c(NA, -2L), class = "data.frame")
对于我的输出,我遍历id并绘制与每个id
对应的多边形列表,所以像这样(没有我的循环):
ggplot() +
geom_sf(data = df[df$id=="A1",])
我想一般地标记每个多边形列表,因此对于每个图,我将具有“片段1”,“片段2” ...等。就像在我的第一张图片中一样,取决于片段数(在下面的示例中,id = A1为3)。
似乎很基本,但无法弄清楚吗?
答案 0 :(得分:2)
出于绘制目的,您需要做的是使用st_cast
将MULTIPOLYGON
几何图形拆分为多个POLYGON
几何图形。默认情况下,这会警告您正在跨几何图形复制属性(此处为id
),在这种情况下可以这样做,但根据属性可能会导致错误(请勿复制面积之类的度量!)每行只有一个几何图形,使用geom_sf_label
以相同的方式进行绘制变得很容易。
library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
df <- structure(list(id = structure(1:2, .Label = c("A1", "A2"), class = "factor"), geometry = structure(list(structure(list(list(structure(c(0, 1, 3, 2, 1, 0, 0, 0, 2, 4, 4, 0), .Dim = c(6L, 2L)), structure(c(1, 1, 2, 1, 1, 2, 2, 1), .Dim = c(4L, 2L))), list(structure(c(3, 4, 4, 3, 3, 0, 0, 1, 1, 0), .Dim = c(5L, 2L)), structure(c(3.3, 3.3, 3.8, 3.8, 3.3, 0.3, 0.8, 0.8, 0.3, 0.3), .Dim = c(5L, 2L))), list(structure(c(3, 4, 4, 3, 3, 2, 3, 3), .Dim = c(4L, 2L)))), class = c("XY", "MULTIPOLYGON", "sfg")), structure(list(list(structure(c(0, 1, 3, 2, 1, 0, 0, 0, 2, 4, 4, 0), .Dim = c(6L, 2L)), structure(c(1, 1, 2, 1, 1, 2, 2, 1), .Dim = c(4L, 2L))), list(structure(c(3, 4, 4, 3, 3, 0, 0, 1, 1, 0), .Dim = c(5L, 2L)), structure(c(3.3, 3.3, 3.8, 3.8, 3.3, 0.3, 0.8, 0.8, 0.3, 0.3), .Dim = c(5L, 2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), crs = structure(list(epsg = NA_integer_, proj4string = NA_character_), class = "crs"), n_empty = 0L, precision = 0, bbox = structure(c(xmin = 0, ymin = 0, xmax = 4, ymax = 4), class = "bbox"), class = c("sfc_MULTIPOLYGON", "sfc"))), row.names = c(NA, -2L), class = "data.frame")
df %>%
st_as_sf %>%
st_cast("POLYGON", group_or_split = TRUE, warn = FALSE) %>%
ggplot() +
geom_sf(aes(fill = id)) +
geom_sf_label(aes(label = id))
在您的示例中,这看起来有点奇怪,因为A2
多边形与A1
多边形相同,因此颜色和标签被遮盖了。确实绘制了3个A1
标签,如果在filter(id == "A1")
调用之前添加一行ggplot
,则可以看到此标签:
df %>%
st_as_sf %>%
st_cast("POLYGON", group_or_split = TRUE, warn = FALSE) %>%
filter(id == "A1") %>%
ggplot() +
geom_sf(aes(fill = id)) +
geom_sf_label(aes(label = id))
由reprex package(v0.2.1)于2019-05-13创建