[GIS] R tmap: how to remove blank space when using `tm_facets()`

r

I am using R package tmap to plot maps with multiple facets, using tm_facets().

It seems like space is not maximally used: there is a lot of blank/unused space on the resulting plot. Is that a constraint due to the specific size of my maps, or can I adjust it with some parameters?

library(tmap)
library(tidyverse)
data(metro)
metro_long <- metro %>% 
  gather(year, population, starts_with("pop"))


metro_long %>% 
  mutate(population = log(population)) %>% 
  filter(year %in% c("pop2010", "pop2020", "pop2030")) %>% 
  tm_shape()+
  tm_dots("population", size = 0.2, legend.is.portrait = FALSE) +
  tm_facets("year") +
  tm_layout(legend.outside.position =  "bottom" )
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3

Best Answer

While the original poster did not specify exactly which the blank spaces were problematic, the plotting area for each map has a lot of space on the right and left (east-west) that is unused. The main issue there is that tmap is using up all the space in each map in the plotting area (often defined by the size of the window available for plotting, this can be manually adjusted prior to running the plot command) provided in its default settings.

To set the bounds of the plotting area, adjust the aspect ratio, i.e. the width and height desired. Here, if we specify an asp=4, we get a plot that roughly corresponds to the map area. A value of asp=8 gives us something similar to the default plot. The result has a lot of blank space on either side of the maps/plot: either crop the exported image or set the exact size in a tmap_save.

Aspect ratio. The aspect ratio of the map (width/height). If NA, it is determined by the bounding box (see argument bbox of tm_shape), the outer.margins, and the inner.margins. If 0, then the aspect ratio is adjusted to the aspect ratio of the device.

metro_long %>% 
  mutate(population = log(population)) %>% 
  filter(year %in% c("pop2010", "pop2020", "pop2030")) %>% 
  tm_shape()+
  tm_dots("population", size = 0.2, legend.is.portrait = FALSE) +
  tm_facets("year") +
  tm_layout(legend.outside.position =  "bottom",
            asp=4)

enter image description here

Related Question