英文:
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")
Created on 2023-01-08 with reprex v2.0.2
英文:
Use data =
like
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")
<!-- -->
<sup>Created on 2023-01-08 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论