Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R sf ggplot标记ID中列出的多个多边形_R_List_Ggplot2_Sf - Fatal编程技术网

R sf ggplot标记ID中列出的多个多边形

R sf ggplot标记ID中列出的多个多边形,r,list,ggplot2,sf,R,List,Ggplot2,Sf,假设我想要打印和标记多边形(数据帧中每行一个多边形),我可以执行以下操作: 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_

假设我想要打印和标记多边形(数据帧中每行一个多边形),我可以执行以下操作:

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))
对于我的输出,我正在循环id并绘制对应于每个
id
的多边形列表,因此类似这样的内容(没有我的循环):

我想给每个多边形列表加上标签,所以对于每个图,我会有“片段1”,“片段2”…等等。就像在我的第一张图片中一样,这取决于片段的数量(下面的示例中,id=A1为3)。


看起来很基本,但无法理解它?

为了打印,您需要做的是使用
st_cast
将您的
多多边形
几何体拆分为多个
多边形
几何体。默认情况下,这将警告您正在跨几何体复制属性(此处为
id
),在这种情况下这很好,但根据属性可能会导致错误(不要复制像面积这样的测量!)一旦我们每行有一个几何体,就可以很容易地使用
geom\u sf\u标签
以相同的方式进行打印

库(tidyverse)
图书馆(sf)
#>链接至GEOS 3.6.1、GDAL 2.1.3、项目4.9.3
df%
st_as_sf%>%
st_cast(“多边形”,组或分割=真,警告=假)%>%
ggplot()+
geom_sf(aes(填充=id))+
geom_sf_标签(aes(标签=id))

在您的示例中,这看起来有点奇怪,因为
A2
多边形与
A1
多边形相同,因此颜色和标签被遮盖。确实绘制了3个
A1
标签,如果在
ggplot
调用之前添加一行
filter(id==“A1”)
可以看到:

df%>%
st_as_sf%>%
st_cast(“多边形”,组或分割=真,警告=假)%>%
筛选器(id==“A1”)%>%
ggplot()+
geom_sf(aes(填充=id))+
geom_sf_标签(aes(标签=id))


由(v0.2.1)于2019-05-13创建,谢谢你,你只是缺少了一行
变异(c=paste(“Fragment”,seq_-along(id),sep=”“)
,它改变了
geom_-sf_标签(aes(label=c))
,因为我想以不同的方式命名每件作品,而不是完全相同。您知道如何使用循环引用
id
中的每个标签吗,比如:
geom\u sf\u标签(aes(label=df[df$id==i,c“])
计算出来。
geom\u sf\u标签(data=df[df$id==i,],aes(label=c))
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")
ggplot() + 
  geom_sf(data = df[df$id=="A1",])