如何在 R 中组合 sf 元素层

问题描述

尽管一些使用 R 的经验,但我在使用 R 执行类似 GIS 的任务时经验要少得多。 我有德国境内所有社区的 shapefile,并创建了一个显示德国 16 个州边界的新对象。

gem <- readOGR(path/to/shapefile.shp) # reading shapefile
gemsf <- st_read(path/to/shapefile.shp) # reading shapefile as sf object


f00 <- gUnaryUnion(gem,id = gem@data$SN_L) # SN_L is the column of the varIoUs states - this line creates a new sp object with only the states instead of all communities

f002 <-  sf::st_as_sf(f00,coords = c("x","y")) # turning the object into an sf object,so graphing with ggplot is easier

为了检查我到目前为止的工作,我使用

绘制了基本数据(社区)
gemsf %>%
ggplot(data = .,) + geom_sf( aes(fill = SN_L)) # fill by state

以及创建 16 个州的图的 plot(f002),而 ggplot-code 提供了一个很好的德国社区地图,每个州都填充了不同的颜色。

现在我想用指示州边界的第二层覆盖它(所以如果你例如绘制人口密度,你仍然可以轻松区分州)。

我尝试这样做,我使用了“标准程序”并添加了另一层

ggplot() + 
geom_sf(data = gemsf,aes(fill = SN_L)) +   # fill by state
geom_sf(data = f002) # since the f002 data frame/sf object ONLY has a geometry column,there is no aes()

导致以下输出https://i.ibb.co/qk9zWRY/ggplot-map-layer.png

那么如何添加仅提供边框而不覆盖下面感兴趣的实际层的第二层?在 QGIS 或 ArcGIS 中,这是常见的过程而不是问题,我也希望能够在 R 中重新创建它。

非常感谢您的帮助!

解决方法

我找到了一个解决方案,想与大家分享。

ggplot() + 
  geom_sf(data = gemsf_data,aes(fill = log(je_km2))) +   # fill by state
  geom_sf(data = f002,alpha = 0,color = "black") + # since the f002 data frame/sf object ONLY has a geometry column,there is no aes()
  theme_minimal()

诀窍不是在 aes() 部分添加“alpha”,而是如上所示。