在现有 geom_sf 层下方插入 geom_sf 层
Insert geom_sf layer underneath existing geom_sf layers
我有一张印度的基本地图,其中包含各州和边界、一些标签以及许多其他规格,这些规格存储为一个 gg 对象。我想生成一些带有区域层的地图,这些地图将承载来自不同变量的数据。
为了防止地区地图覆盖州和国家边界,它必须在前面所有代码之前,我想避免重复。
我想我可以通过根据 this answer 为 gg 对象调用 $layers
来做到这一点。但是,它会引发错误。 Reprex 如下:
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
预期结果
如有任何帮助,我们将不胜感激!
如果您查看 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)
我仍然不确定我是否遗漏了您正在寻找的细节,但是 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()
...
然后您可以按照需要显示的顺序添加图层来绘图。请记住,如果您需要对这些 sf
中的任何一个进行其他计算,您可以提前或在 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 创建
我有一张印度的基本地图,其中包含各州和边界、一些标签以及许多其他规格,这些规格存储为一个 gg 对象。我想生成一些带有区域层的地图,这些地图将承载来自不同变量的数据。
为了防止地区地图覆盖州和国家边界,它必须在前面所有代码之前,我想避免重复。
我想我可以通过根据 this answer 为 gg 对象调用 $layers
来做到这一点。但是,它会引发错误。 Reprex 如下:
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
预期结果
如有任何帮助,我们将不胜感激!
如果您查看 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)
我仍然不确定我是否遗漏了您正在寻找的细节,但是 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()
...
然后您可以按照需要显示的顺序添加图层来绘图。请记住,如果您需要对这些 sf
中的任何一个进行其他计算,您可以提前或在 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 创建