【问题标题】:How to combine sf elements (layers) in R如何在 R 中组合 sf 元素(层)
【发布时间】:2021-03-08 12:42:10
【问题描述】:

尽管有一些使用 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

还有 plot(f002) 创建了 16 个州的图,而 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 中重新创建它。

非常感谢您的帮助!

【问题讨论】:

    标签: r maps gis geospatial


    【解决方案1】:

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

    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”,而是如上所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 2021-05-17
      • 1970-01-01
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多