knitr::opts_chunk$set(echo = TRUE)

Quick Tutorial on Using the Tmap Package

Libraries

library( tmap )
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(magrittr) #pipe operator
library(pander) #pander

Data

World will be our dataset for creating a map.

data("World")

World %>% as.data.frame() %>% head()
##   iso_a3                 name           sovereignt     continent
## 1    AFG          Afghanistan          Afghanistan          Asia
## 2    AGO               Angola               Angola        Africa
## 3    ALB              Albania              Albania        Europe
## 4    ARE United Arab Emirates United Arab Emirates          Asia
## 5    ARG            Argentina            Argentina South America
## 6    ARM              Armenia              Armenia          Asia
##                area  pop_est pop_est_dens                   economy
## 1  652860.00 [km^2] 28400000     43.50090 7. Least developed region
## 2 1246700.00 [km^2] 12799293     10.26654 7. Least developed region
## 3   27400.00 [km^2]  3639453    132.82675      6. Developing region
## 4   71252.17 [km^2]  4798491     67.34519      6. Developing region
## 5 2736690.00 [km^2] 40913584     14.95003   5. Emerging region: G20
## 6   28470.00 [km^2]  2967004    104.21510      6. Developing region
##                income_grp gdp_cap_est life_exp well_being footprint inequality
## 1           5. Low income    784.1549   59.668        3.8      0.79  0.4265574
## 2  3. Upper middle income   8617.6635       NA         NA        NA         NA
## 3  4. Lower middle income   5992.6588   77.347        5.5      2.21  0.1651337
## 4 2. High income: nonOECD  38407.9078       NA         NA        NA         NA
## 5  3. Upper middle income  14027.1261   75.927        6.5      3.14  0.1642383
## 6  4. Lower middle income   6326.2469   74.446        4.3      2.23  0.2166481
##        HPI                       geometry
## 1 20.22535 MULTIPOLYGON (((61.21082 35...
## 2       NA MULTIPOLYGON (((16.32653 -5...
## 3 36.76687 MULTIPOLYGON (((20.59025 41...
## 4       NA MULTIPOLYGON (((51.57952 24...
## 5 35.19024 MULTIPOLYGON (((-65.5 -55.2...
## 6 25.66642 MULTIPOLYGON (((43.58275 41...
# We will also use metros, rivers, and land, as you'll see below

Data Dictionary

world.data.dictionary <- 
structure(list(LABEL = c("iso_a3", "name", "sovereignt", "continent", 
"area", "pop_est", "pop_est_dens", "economy", "income_grp", "gdp_cap_est", 
"life_exp", "well_being", "footprint", "inequality", "HPI", "geometry"), 

VARIABLE = c("Country Name Abbreviation", "Country Name", "Sovereignt", "Continent", 
"Area Size", "Population Estimate", "Population Density Estimate", "Economy", "Income by Group", "GDP Estimate", 
"Life Expectency", "Well Being", "Footprint", "Inequality", "Happiness Index", "Geometry")), class = "data.frame", row.names = c(NA, 
-16L))

world.data.dictionary %>% pander()
LABEL VARIABLE
iso_a3 Country Name Abbreviation
name Country Name
sovereignt Sovereignt
continent Continent
area Area Size
pop_est Population Estimate
pop_est_dens Population Density Estimate
economy Economy
income_grp Income by Group
gdp_cap_est GDP Estimate
life_exp Life Expectency
well_being Well Being
footprint Footprint
inequality Inequality
HPI Happiness Index
geometry Geometry

Static maps with tmap

tm_shape(World) +
  tm_fill()

tm_shape(World) +
  tm_borders()

tm_shape(World) +
  tm_polygons()

Adding layers to map

# In order to plot it in tmap, you first need to specify it with tm_shape. 
# Layers can be added with the + operator, in this case tm_polygons.
# You can use tm_ as a prefix to see all the options for map layers. 
# For a full color palette, visit: 
# https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf

#Choose any from the list above

#Economy 
tm_shape(World) +
    tm_borders("white") +
    tm_polygons("economy", palette="-Greens", contrast=.7, id="name", title="Economy")
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

#HPI (Happiness Index)
tm_shape(World) +
  tm_borders("white") +
  tm_polygons("HPI", palette="-Pastel2", contrast=.7, id="name", title="Happiness Index")
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

Multiple shapes and layers

A shape is a spatial object (with a class from sf, sp, stars, or raster).

# Multiple shapes and also multiple layers per shape can be plotted.

data(World, metro, rivers, land)

#Example 1: Population & Text 
tm_shape(World) +
    tm_text("iso_a3", size = "AREA") +
tm_shape(metro) +
    tm_symbols(col = "red", size = "pop2020", scale = .5) +
tm_legend(show = TRUE) # add a legend

#Example 2: Population & Economy
tm_shape(World) +
    tm_polygons("economy", palette="-Greens", contrast=.7, id="name", title="Economy") +
  tm_shape(metro) +
    tm_symbols(col = "white", size = "pop2020", scale = .5) +
tm_legend(show = TRUE) # add a legend

Citation

I am no expert in this area, but since we used tmap this class I thought I would dig into it a bit and share with everyone what I found. You can learn more from the following links:

  1. https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html
  2. https://bookdown.org/nicohahn/making_maps_with_r5/docs/tmap.html
  3. https://tlorusso.github.io/geodata_workshop/tmap_package
  4. https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf