Home > Software design >  Multiple layer map with legend
Multiple layer map with legend

Time:10-01

I am trying to get a map from 2 different layers. The first one represent country borders, the second one are grid-cells over the same area.

Drawing the map itself isn't much of an issue :

ggplot()  
  geom_sf(data=country, color = "black", fill = "white", size=1) 
  geom_sf(data=grid, color = "black", fill = NA)

The code allow to get a map with the country borders with a thick black line and a white background. Then the grid layer is added with a thinner black line and a transparent color background to avoid hidding the country borders from the first layer.

I would like a 2-entry legend called "Legend", the first entry being the thick black lined/white background "Country border" and the second entry being the thinner black lines for "Grid border".

After trying for a few hours, I cannot get something that get close to this. I understand I need to tell R to link objets to their representation using AES statement, but each time I tried the whole map was changed (no borders, only grid-cell borders etc).

I'm not used to work wwith map in R so I don't really know how to provide a MWE in this case.

Thanks.

CodePudding user response:

I don't have the data your working with, so here is an example. All you need to do is put some term in aes that is not already used for defining features (e.g., size, shape, color, fill, alpha, etc.). Usually you do not quote anything in aes, but we do here just to put it in the legend. If you want to mess with the aesthetics of the legend, you can then further edit using guides.

library(tidyverse)
library(sf)


usa <- map_data("usa") |>
  st_as_sf(coords = c("long", "lat"),
           crs = 4326)|>
  group_by(group)|>
  summarise(geometry = st_combine(geometry)) |>
  st_cast("POLYGON")

AL <- map_data("state") |>
  filter(region == "alabama")|>
  st_as_sf(coords = c("long", "lat"),
           crs = 4326)|>
  summarise(geometry = st_combine(geometry)) |>
  st_cast("POLYGON")

ggplot() 
  geom_sf(data=usa, color = "black", fill = "white", size=1, aes(shape = "Country border")) 
  geom_sf(data=AL, color = "black", fill = NA, aes(alpha = "Grid border")) 
  labs(shape = "", alpha = "")

CodePudding user response:

Here's a different example using a country and a grid:

library(ggplot2)

ggplot()  
  geom_sf(data = country, aes(fill = "Country border"), size = 1) 
  geom_sf(data = grid, fill = NA, aes(color = "Grid border"), size = 0.25)  
  scale_fill_manual(values = "white", guide = "legend", name = "Legend")  
  scale_color_manual(values = "black", guide = "legend", name = NULL)  
  guides(fill = guide_legend(order = 1))  
  theme(legend.spacing.y = unit(0, "mm"),
        legend.title = element_text(margin = margin(0, 0, 15, 0)))

enter image description here


Data used

library(sf)
library(rnaturalearth)

country <- ne_countries(10, country = "United Kingdom", returnclass = "sf")

pts <- lapply(c(-10, -5, 0), function(x) matrix(c(x, x, 48, 62.5), nrow = 2))
pts <- c(pts, lapply(2 * 24:31, function(x) matrix(c(-12, 2, x, x), nrow = 2)))
grid <- st_sf(st_sfc(st_multilinestring(pts), crs = st_crs(country)))
  • Related