Section 5.5 Context
Besides the orientation and scale indicators, there are other ways to give context to your map, that is situate it within the wider environment, and put things into perspective for your map readers. In this section we will touch on basemaps, although this is something we have already encountered in great detail in earlier chapters, here we illustrate how to add basemaps in ggplot. We also introduce inset maps, as a way of highlighting where your map sits in the wider context.
Subsection 5.5.1 Basemap
As mentioned above, we have encountered and included basemaps in exercises in previous chapters. For the sake of illustration, we can add a basemap now using the
annotation_map_tile() function also from the ggspatial package (like the orientation and scale indicators). Make sure that the basemap is the first layer added to the map, so that all subsequent layers are drawn on top of it. If we were to add annotation_map_tile() last, it would cover all the other layers.
ggplot(data = hu_dd) +
annotation_map_tile() + # add basemap layer first
geom_sf(aes(fill = total_quantiles), lwd = 0.5, col = "white") +
scale_fill_brewer(type = "seq", palette = "Greens", name = "Total tests")

This provides one way to add context. In previous iterations, we have adjusted the opacity of our other layers, in order to aid visibility of the basemap underneath them, so this might be something to consider.
Subsection 5.5.2 Inset maps
Inset maps provide another approach to situating your map in context. You might use this to show where your main map fits into the context of a larger area; for example, here we might illustrate how Hungary is situated within Europe. You might also use an inset map in another situation, where you have additional areas which you want to show which may be geographically far but politically related to your region. For example, we might want to portray a map of the United States of America and make sure to include Hawaii and Alaska on the map. The basic principles behind these maps are the same. Essentially we must create two map objects, and then bring these together. Let’s illustrate how.
First, we need to create the map we will be displaying in the inset map. In this case, let’s highlight the location of Hungary on a map of Europe. We can do this by creating a map of Europe (let’s use the
rnaturalearth package for this). We create a list of the countries from the world map countries110, and filter only Europe (we also exclude Russia, because it is so big it makes the rest of Europe hard to see on a smaller map, and Iceland as it’s far, also making the map bigger than we need).
library(rnaturalearth)
europe_countries <- st_as_sf(countries110) %>% # get geom for all countries
janitor::clean_names() %>%
filter(region_un=="Europe" & # select Europe
name != "Russia" & name != "Iceland") %>% # remove Russia and Iceland
pull(name) # get only the names in a list
europe <- ne_countries(geounit = europe_countries, # get geoms for countries in list
type = 'map_units', # country type as map_units
returnclass = "sf") # return sf object (not sp)
Now we can use the returned sf object
europe to create a map of Europe. But this isn’t necessarily enough context. We also want the inset map to highlight Hungary within this map. We can do this by creating another layer, with only Hungary, and making its border red and use a thicker line width. By layering this on top of the Europe map, we are essentially highlighting our study region.
inset_map <- ggplot() + # create new ggplot
geom_sf(data = europe, # add europe map as first layer
fill = "white") + # white fill
geom_sf(data = europe %>% filter(name == "Hungary"), # new layer only Hungary
fill = "white" , # white fill
col = "red", # make the border red
lwd = 2) + # make border line thick
theme_void() + # strip grid elements
theme(panel.border = element_rect(colour = "black", # draw border around map
fill=NA))
We now have this separate map, which highlights where Hungary can be found, right there in Central Europe. To display this jointly with our map of the breathalyser test, we must join the two maps. For this, we will need both as separate objects. We’ve already assigned our inset map to the object
inset_map, and we also have our main map object we’ve been working with, called map.
So now we have our inset map and main map stored as two map objects. To display them together, we can use the
ggdraw() and draw_plot() functions from the cowplot package. Let’s load this package.
library(cowplot)
First, we set up an empty drawing layer for our ggplot using the
ggdraw() function. Then we layer on the two maps both using the draw_plot() function. This allows us to draw plots and subplots. This function places a plot (which we specify as the first parameter of this function) somewhere onto the drawing canvas. By default, coordinates run from 0 to 1, and the point (0, 0) is in the lower left corner of the canvas. We want to therefore specify where the plots go on our canvas explicitly. Alongside position, we can also specify size. This is important, as we usually make the inset map smaller.
hu_dd_with_inset <- ggdraw() + # set layer
draw_plot(map) + # draw the main map
draw_plot(inset_map, # draw inset map
x = 0.75, # specify location on x axis
y = 0, # specify location on y axis
width = 0.35, # specify width
height = 0.35) # specify height
We now have our final map, which we can check out now:
hu_dd_with_inset

You can play around with where you position your inset map by adjusting the x and y coordinates. You can also play around with the size of it by adjusting the parameters for height and width. And as mentioned above, you can use inset maps not only for context, but also to include geographically far away regions which belong to the same political unity, for example to include Alaska and Hawaii in maps of the United States.
