【发布时间】:2021-12-25 23:48:26
【问题描述】:
我对 R 很陌生,对使用 sf 和 ggplot2 包在 R 上绘图的 GIS 非常陌生。我有一个数据集“comuni”,其中包含意大利的所有公社(类似于县)和意大利所有高速公路之一,称为“only_motorway”。我知道我可以将某些区域用作千篇一律的工具,并使用 st_intersection() 函数仅保留这些区域中包含的高速公路。但是,我想反其道而行之,鉴于我有 A3 高速公路的 shapefile,我只想保留那些经过该特定高速公路的公社。
我尝试通过以下方式使用 st_intersection 函数:
only_motorway_A3 <- only_motorway %>%
filter(ref == "A3")
comuni_A3 <- st_intersection(only_motorway_A3,comuni)
ggplot() +
geom_sf(data = comuni_A3,
color = "black", size = 0.1, fill = "black") +
geom_sf(data = only_motorway_A3, size = 0.15, color = "green") +
coord_sf(crs = 32632) +
theme_void()
但结果如下图:
即 only_motorway_A3 和 comuni_A3 具有相同的几何列,并且它们都绘制高速公路线。我想要绘制的是来自 only_motoway_A3 的高速公路线(绿色)以及它周围从 comuni_A3 穿过的公社(黑色)。我希望它很清楚,并提前感谢您的帮助!
【问题讨论】:
-
我认为你想要
st_intersects而不是st_intersection。如果你使用st_intersects,你会得到一个真假逻辑向量,这将允许你对comuni进行子集化