4

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

2

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

1

The argument to set this is legend.outside.size. The default depends on which style is used, but for the default style, the value is 0.3 (check tmap_options()$legend.outside.size).

Setting this to a smaller 0.1 value will make the legend take less space. Note however that the font of the legend is also reduced though, so there's a trade-off in the ideal legend.outside.size value.

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

tm_default <- 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", nrow=3) +
  tm_layout(legend.outside.position =  "bottom", main.title="Default size (0.3)")

tm_small <- tm_default+ tm_layout(legend.outside.size=0.1,
                                  main.title="Manual size 0.1")

tmap_arrange(tm_default, tm_small)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.