I have two polygon '.shp' files. I need one to appear on the map by filling in one variable and the other to appear only on borders, overlapping the first.
I have already used 'ggplot2' and 'sf'.
I plotted a map ('map1'), which is layered with polygons, using 'ggplot' and 'geom_sf'.
I use a variable ('var1') contained in 'map1' as a 'fill'.
Now, I need to add (overlay) another layer of polygons on top ('map2'). This will have to be 'transparent fill' or 'no fill'. Only appearing the outline of the borders.
library(ggplot2); library(sf)
map1 <- st_read("m1.shp") #reading polygon layer map 1
map2 <- st_read("m2.shp")#reading polygon layer map 2
g <- ggplot(map1, aes(fill = var1)) +
geom_sf()
How can i add 'map2' for overlay this map?
The idea would be:
g <- ggplot(map1, aes(fill = var1)) +
geom_sf() +
ggplot(map2, aes()) +
geom_sf()
#Error: Don't know how to add ggplot(map2, aes()) to a plot
Every geom_SOMETHING() function has a data argument where you can configure the data you are using. This argument plays the same role as the data argument in the ggplot() function. When you specify data in ggplot, all the other geom_SOMETHING() function inherit the argument. The same happens with the arguments in aes()
So the first recommendation is remove the data = map1 and aes arguments from ggplot and add it to the geom_sf function.
g <- ggplot() + geom_sf(data = map1, aes(fill = var1)) + geom_sf(data = map2)