【问题标题】:Insert geom_sf layer underneath existing geom_sf layers在现有 geom_sf 图层下方插入 geom_sf 图层
【发布时间】:2018-10-28 23:11:42
【问题描述】:

我有一张印度的基本地图,其中包含州和边界、一些标签以及存储为 gg 对象的许多其他规范。我想生成一些带有区域图层的地图,这些地图将包含来自不同变量的数据。

为了防止地区地图覆盖州和国家边界,它必须在所有之前的代码之前,我想避免重复。

我想我可以按照this answer 为 gg 对象调用$layers 来做到这一点。但是,它会引发错误。代表如下:

library(ggplot2)
library(sf)
library(raster)

# Download district and state data (should be less than 10 Mb in total)

distSF <- st_as_sf(getData("GADM",country="IND",level=2))

stateSF <- st_as_sf(getData("GADM",country="IND",level=1))

# Add border

countryborder <- st_union(stateSF)

# Basic plot

basicIndia <- ggplot() +
  geom_sf(data = stateSF, color = "white", fill = NA) +
  geom_sf(data = countryborder, color = "blue", fill = NA) +
  theme_dark()

basicIndia

# Data-bearing plot

districts <- ggplot() +
  geom_sf(data = distSF, fill = "gold")

basicIndia$layers <- c(geom_sf(data = distSF, fill = "gold"), basicIndia$layers)

basicIndia
#> Error in y$layer_data(plot$data): attempt to apply non-function

预期结果

任何帮助将不胜感激!

【问题讨论】:

  • 我想我错过了一些东西。既然你有 3 个独立的 sf 对象,为什么不直接绘制地区 + 州 + 国家?
  • 这可能是我自己的无知。我有很多代码可以修改州和国家/地区的外观,这些代码不会改变并且必须重复。这在上面的代表中由basicIndia 表示。如果我这样做ggplot(districts) + basicIndia 它会抛出一个错误,因为我正在绘制一个 gg 对象,而不是一个 df。我不知道如何存储部分 ggplot 以重新组合它们。

标签: r ggplot2 sf


【解决方案1】:

我仍然不确定我是否遗漏了您正在寻找的细节,但ggplot2 按照您提供的顺序绘制图层。所以像

ggplot(data) +
  geom_col() +
  geom_point(...) +
  geom_line(...)

将绘制列,然后在这些列的顶部绘制点,然后在前一层的顶部绘制线。

sf 的地块也是如此,这样可以很容易地制作这样一个具有多个地理级别的地块。

(我在sf 对象上使用rmapshaper::ms_simplify 只是为了简化它们并加快绘图速度。)

library(dplyr)
library(ggplot2)
library(sf)
library(raster)

distSF <- st_as_sf(getData("GADM",country="IND",level=2)) %>% rmapshaper::ms_simplify()
...

然后您可以按照需要显示的顺序添加图层来进行绘图。请记住,如果您需要对这些 sfs 中的任何一个进行其他计算,您可以提前或在您的 geom_sf 中进行。

ggplot() +
  geom_sf(data = distSF, fill = "gold", size = 0.1) +
  geom_sf(data = stateSF, color = "white", fill = NA) +
  geom_sf(data = countryborder, color = "blue", fill = NA)

关于尝试将一个绘图添加到另一个绘图:ggplot2 在图层中工作,因此您创建一个基础 ggplot 对象,然后在其上添加几何图形。因此,例如,您可以制作两个有效的图:

state_plot <- ggplot(stateSF) + 
  geom_sf(color = "white", fill = NA)
country_plot <- ggplot(countryborder) + 
  geom_sf(color = "blue", fill = NA)

但您不能添加它们,因为您将有 2 个基本 ggplot 对象。这应该是你提到的错误:

state_plot + 
  country_plot
#> Error: Don't know how to add country_plot to a plot

相反,如果您需要绘制绘图,然后在其上添加其他内容,创建基础 ggplot,然后添加几何图层,例如带有不同数据集的 geom_sf

state_plot +
  geom_sf(data = countryborder, fill = NA, color = "blue")

reprex package (v0.2.1) 于 2018 年 10 月 29 日创建

【讨论】:

    【解决方案2】:

    如果您查看geom_sf(data=distSF),您会发现它是一个由两个元素组成的列表 - 您想要第一个包含层信息的元素,所以geom_sf(data = distSF, fill = "gold")[[1]] 应该可以工作。

    districts <- ggplot() +
      geom_sf(data = distSF, fill = "gold")
    
    basicIndia$layers <- c(geom_sf(data = distSF, fill = "gold")[[1]], basicIndia$layers)
    

    【讨论】:

    • 谢谢你解决了第一阶段......不过我不得不问第二个问题......xD
    猜你喜欢
    • 2023-01-09
    • 1970-01-01
    • 2021-03-13
    • 2021-05-24
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多