【发布时间】:2019-09-30 16:33:45
【问题描述】:
假设我想绘制和标记多边形(数据框中每行一个多边形),我可以执行以下操作:
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)。
看起来很基本,但无法弄清楚?
【问题讨论】: