Skip to contents

About the data

A spatial data object contained in tmap is called World. It is a data frame with a row for each country. The columns are the following data variables plus an additional geometry column which contains the geometries (see sf package):

names(World)
#>  [1] "iso_a3"       "name"         "sovereignt"   "continent"    "area"        
#>  [6] "pop_est"      "pop_est_dens" "economy"      "income_grp"   "gdp_cap_est" 
#> [11] "life_exp"     "well_being"   "footprint"    "HPI"          "inequality"  
#> [16] "gender"       "press"        "geometry"

We specify this object with tm_shape (see other vignette) and for convenience assign it to s:

s = tm_shape(World, crs = "+proj=eqearth")

Formatting legend labels

Single numbers

Numbers of numeric legends are formatted with the label.format argument of the scale function, e.g. tm_scale_continuous:

s + tm_polygons("HPI", fill.scale = tm_scale_continuous(values = "pu_gn_div"))

s + tm_polygons("HPI", fill.scale = tm_scale_continuous(values = "pu_gn_div", label.format = tm_label_format(digits = 3)))

s + tm_polygons("HPI", fill.scale = tm_scale_continuous(values = "pu_gn_div", label.format = tm_label_format(scientific = TRUE)))

s + tm_polygons("HPI", fill.scale = tm_scale_continuous(values = "pu_gn_div", label.format = tm_label_format(prefix = "<", suffix = ">")))

s + tm_polygons("HPI", fill.scale = tm_scale_continuous(values = "pu_gn_div", label.format = tm_label_format(fun = function(x) {
    lst = strsplit(sprintf("%05d", x), split = "")
    sapply(lst, function(y) {
        do.call(paste, c(as.list(y), list(collapse = " ")))
    })
})))

Intervals

For intervals there are a few other options.

Consider this map

s + tm_polygons(
  fill = "HPI",
  fill.scale = tm_scale_intervals(breaks = seq(10, 60, by = 10)))

First of all note that intervals are by default closed on the left-handside, so the first interval represents all values from (and including) 10 to (and not including) the next break which is 20. How intervals are closed is configured with interval.closure.

Apart from how intervals are closed, another choice is how to present them. In the old tmap version (<= 4.1), they were labeled as "10 to 20", "20 to 30". This lead to a lot of confusion because it was unclear where 20 belonged to. This old option can be enabled via the argument label.disjoint of tm_label_format:

s + tm_polygons(
  fill = "HPI",
  fill.scale = tm_scale_intervals(breaks = seq(10, 60, by = 10), label.format = tm_label_format(interval.disjoint = FALSE)))

With the new default (interval.disjoint = TRUE) the argument digits can be used additionally:

s + tm_polygons(
  fill = "HPI",
  fill.scale = tm_scale_intervals(breaks = seq(10, 60, by = 10), label.format = tm_label_format(digits = 2)))

Use -Inf for the first break and Inf for the last break to trigger labels that reflect “less than” and “or more” respectively. How these are formatted can be specified with label.format:

s + tm_polygons(
  fill = "HPI",
  fill.scale = tm_scale_intervals(
    n = 6,
    style = "fixed",    
    breaks = c(-Inf,10,20,30,40,50, Inf),
    values = "pu_gn_div",
    label.format = tm_label_format(text.separator = "to", 
                                   text.less.than = "less than", 
                                   text.less.than_as.prefix = TRUE,
                                   text.or.more = "or more", 
                                   text.or.more_as.prefix = FALSE)
  ))

Big numbers

By default, big numbers are abbreviated. If differences among legend values are sufficiently large, they are presented in millions, billions etc.

s + tm_polygons() +
    tm_bubbles(size = "pop_est")

The big number abbreviations are specified in big.num.abbr. The default value is

tmap_options("label.format")[[1]]$big.num.abbr
#> mln bln trn qdn qnt 
#>   6   9  12  15  18

Instead, we can define the population count in ‘hundred-thousands’:

s + tm_polygons() +
    tm_bubbles(size = "pop_est",
               size.scale = tm_scale_continuous(label.format = tm_label_format(big.num.abbr = c(hk = 5))))

Multiple visual variables

Neutral values