Home > Net >  R: Inserting a geom_sf layer on top of another geom_sf layer
R: Inserting a geom_sf layer on top of another geom_sf layer

Time:01-09

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?

CodePudding user response:

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")

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

  • Related