Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Could some help, please?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading