This vignette provides a detailed description of inset map generation, building on the content in Section Inset maps of the Geocomputation with R book.

## Prerequisites

The example below assume the following packages are attached:

# devtools::install_github("geocompr/geocompkg") # install pkgs
library(spData) # contains datasets used in this example
library(sf)     # for handling spatial (vector) data
library(tmap)   # map making package
library(grid)   # to re-arrange maps


## Data preparation

The first step is to decide on the best projection for each individual inset. For this case, we decided to use equal area projections for the maps of the contiguous 48 states, Hawaii, and Alaska. While the dataset of Hawaii and Alaska already have this type of projections, we still need to reproject the us_states object to US National Atlas Equal Area:

us_states2163 = st_transform(us_states, 2163)


## Ratio calculation

The second step is to calculate scale relations between the main map (the contiguous 48 states) and Hawaii, and between the main map and Alaska. To do so we can calculate areas of the bounding box of each object:

us_states_range = st_bbox(us_states2163)[4] - st_bbox(us_states2163)[2]
hawaii_range = st_bbox(hawaii)[4] - st_bbox(hawaii)[2]


Next, we can calculate the ratios between their areas:

us_states_hawaii_ratio = hawaii_range / us_states_range


## Single maps creation

In the third step, we need to create single maps for each object. We can use the tm_layout() function to get rid of map frames and backgrounds or to add titles to our insets:

us_states_map = tm_shape(us_states2163) +
tm_polygons() +
tm_layout(frame = FALSE)

hawaii_map = tm_shape(hawaii) +
tm_polygons() +
tm_layout(title = "Hawaii", frame = FALSE, bg.color = NA,
title.position = c("left", "BOTTOM"))

alaska_map = tm_shape(alaska) +
tm_polygons() +
tm_layout(title = "Alaska", frame = FALSE, bg.color = NA,
title.position = c("left", "TOP"))


## Map arrangement

The final step is to arrange the map. One of the ways to achieve this goal is to use the grid package. In the code below, we create a new page and specify its layout with two rows - one smaller (for Alaska) and one larger (for the contiguous 48 states). Next, we populate the layout with maps of Alaska and the contiguous 48 states add a map of Hawaii to the bottom left of the map and specify its size. Lastly, we draw two dashed lines (gp = gpar(lty = 2)) to separate the contiguous 48 states from Alaska and Hawaii.

grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1,