I would like to plot a figure with small multiple maps using ggplot2::geom_sf
. The challenge here is how to do this keeping all maps centered in the image and at the same spatial scale. Here is the problem (data for reproducible example below):
A simple map using facet_wrap
put all polygons at the same spatial scale, but they are not centered.
ggplot(states6) +
geom_sf() +
facet_wrap(~name_state)
Here is a solution from this SO question
that uses cowplot
. In this case, polygons are centered but they come at different spatial scales
g <- purrr::map(unique(states6$name_state),
function(x) {
# subset data
temp_sf <- subset(states6, name_state == x)
ggplot() +
geom_sf(data = temp_sf, fill='black') +
guides(fill = FALSE) +
ggtitle(x) +
ggsn::scalebar(temp_sf, dist = 100, st.size=2,
height=0.01, model = 'WGS84',
transform = T, dist_unit='km')
})
g2 <- cowplot::plot_grid(plotlist = g)
g2
I've found the same problem using the tmap
library.
tm_shape(states6) +
tm_borders(col='black') +
tm_fill(col='black') +
tm_facets(by = "name_state ", ncol=3) +
tm_scale_bar(breaks = c(0, 50, 100), text.size = 3)
Desired output
The output I would like to get is something similar to this:
Data for reproducible example
library(sf)
library(geobr)
library(mapview)
library(ggplot2)
library(ggsn)
library(cowplot)
library(purrr)
library(tmap)
# Read all Brazilian states
states <- geobr::read_state(code_state = 'all', year=2015)
# Select six states
states6 <- subset(states, code_state %in% c(35,33,53,29,31,23))