在另一个geom_sf层之上插入一个geom_sf层。

huangapple go评论75阅读模式
英文:

R: Inserting a geom_sf layer on top of another geom_sf layer

问题

我想绘制一个法国地图,使用 geom_sf,然后在地图上添加一个德国的图层。

这是我做的:

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

fr_sf = st_as_sf(getData("GADM", country = "FRA", level = 1))
de_sf = st_as_sf(getData("GADM", country = "DEU", level = 0))

ggplot(fr_sf) +
  geom_sf(fill = "gray90") + 
  geom_sf(de_sf, fill = "gray90")

但是我得到了一个错误消息 "mapping 必须由 aes() 创建"。

我尝试使用 geom_sf(de_sf, fill = "gray90")[[1]],但是我得到了相同的错误。

请问有人可以帮忙吗?

英文:

I would like to plot a map of France using geom_sf and then add a layer to map Germany.

Here's what I did

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

fr_sf = st_as_sf(getData("GADM",country="FRA",level=1))
de_sf = st_as_sf(getData("GADM",country="DEU",level=0))

ggplot(fr_sf) +
  geom_sf(fill = "gray90") + 
  geom_sf(de_sf, fill = "gray90")

but I get an error mapping` must be created by `aes()

I tried with geom_sf(de_sf, fill = "gray90")[[1]], but I got the same error.

Could some help, please?

答案1

得分: 1

使用data = ,如下所示:

library(ggplot2)
library(sf)
# Loading required package: sp

fr_sf <- st_as_sf(getData("GADM", country = "FRA", level = 1))
de_sf <- st_as_sf(getData("GADM", country = "DEU", level = 0))

ggplot(data = fr_sf) +
  geom_sf(fill = "gray90") +
  geom_sf(data = de_sf, fill = "gray90")

在另一个geom_sf层之上插入一个geom_sf层。

Created on 2023-01-08 with reprex v2.0.2

英文:

Use data = like

library(ggplot2)
library(sf)
#  Loading required package: sp

fr_sf &lt;- st_as_sf(getData(&quot;GADM&quot;, country = &quot;FRA&quot;, level = 1))
de_sf &lt;- st_as_sf(getData(&quot;GADM&quot;, country = &quot;DEU&quot;, level = 0))

ggplot(data = fr_sf) +
  geom_sf(fill = &quot;gray90&quot;) +
  geom_sf(data = de_sf, fill = &quot;gray90&quot;)

在另一个geom_sf层之上插入一个geom_sf层。<!-- -->

<sup>Created on 2023-01-08 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年1月9日 03:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定