Introduction

This code through explores the “Tmap” package in R, which is a tool used for visualizing spatial data. It provides functions that allow for users to create thematic maps that represent geographical data in different ways such as, but not limited to, choropleth maps, flow maps, cartograms. The ‘tmap’ package is able to create visualizations by preparing data (which can be generated from various formats) and building the maps using layers - each layer containing various aspects of the map such as labels, data points, or boundaries.


Importance of Tmap

Tmap is a valuable package since it simplifies the creation of spatial data visualization. The user-friendly interface and its diverse abilities to generate various types of maps makes data visualization more accessible to all. Its customizability and integration into the R interface allows for effective creation of spatial patterns. As an open-source package, tmap is a valuable resource for educational, professional, and research purposes as well.


Learning Objectives

Specifically, you’ll learn how to download and install tmap in R and use the package to create a thematic map using an existing data set in R. After creating the basis of the map, you will learn how to add layers to the map to include various components and details.


Further Exposition

This is based/expanded on the theory/work/extension of already existing spatial data visualization concepts such as geographic information systems. Also similar to the package, ggplot2, tmap uses layers to build on maps. Since it was built on R’s spatial data handling capabilites, it can be used with existing spatial packages while also adding certain functions.


Example of Data Preparation

A basic example shows how to load the tmap package and ‘World’ data set to visualize the Human Poverty Index, also known as HPI, across different countries. The ‘World’ data set includes spatial data of all the countries in the world. It provides information such as country names, ISO codes, and the geographical coordinates.

library(tmap)

data(World)


Data preparation is a necessary step in order to explore the data and preview what is available to work with. A summary of the data available in ‘World’ can be viewed by using the summary() function and including the variable that you are looking for.

str(World)
## Classes 'sf' and 'data.frame':   177 obs. of  16 variables:
##  $ iso_a3      : Factor w/ 177 levels "AFG","AGO","ALB",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ name        : Factor w/ 177 levels "Afghanistan",..: 1 4 2 166 6 7 5 56 8 9 ...
##  $ sovereignt  : Factor w/ 171 levels "Afghanistan",..: 1 4 2 159 6 7 5 52 8 9 ...
##  $ continent   : Factor w/ 8 levels "Africa","Antarctica",..: 3 1 4 3 8 3 2 7 6 4 ...
##  $ area        : Units: [km^2] num  652860 1246700 27400 71252 2736690 ...
##  $ pop_est     : num  28400000 12799293 3639453 4798491 40913584 ...
##  $ pop_est_dens: num  43.5 10.3 132.8 67.3 15 ...
##  $ economy     : Factor w/ 7 levels "1. Developed region: G7",..: 7 7 6 6 5 6 6 6 2 2 ...
##  $ income_grp  : Factor w/ 5 levels "1. High income: OECD",..: 5 3 4 2 3 4 2 2 1 1 ...
##  $ gdp_cap_est : num  784 8618 5993 38408 14027 ...
##  $ life_exp    : num  59.7 NA 77.3 NA 75.9 ...
##  $ well_being  : num  3.8 NA 5.5 NA 6.5 4.3 NA NA 7.2 7.4 ...
##  $ footprint   : num  0.79 NA 2.21 NA 3.14 2.23 NA NA 9.31 6.06 ...
##  $ inequality  : num  0.427 NA 0.165 NA 0.164 ...
##  $ HPI         : num  20.2 NA 36.8 NA 35.2 ...
##  $ geometry    :sfc_MULTIPOLYGON of length 177; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:69, 1:2] 61.2 62.2 63 63.2 64 ...
##   ..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
##  - attr(*, "sf_column")= chr "geometry"
##  - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
##   ..- attr(*, "names")= chr [1:15] "iso_a3" "name" "sovereignt" "continent" ...
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"    "inequality"   "HPI"         
## [16] "geometry"
summary(World$HPI)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   12.78   21.21   26.29   26.48   31.73   44.71      41


Example of Data Visualization

After viewing the list of columns available in the World data set, ensure that you are selecting the correct variable name in order to generate the thematic map. HPI, the Human Poverty Index, is an existing column in the data so tm_fill() can be used to input the data into a map.

tm_shape(World) +
  tm_borders() +  
  tm_fill("HPI", style = "quantile", palette = "Greens", title = "Human Poverty Index (HPI)")


As stated above in the introduction, various layers can be added to the thematic map to add context and details. In this layer, we will add labels of the country names and adjust the font size and color of both the countries’ labels and the legend.

tm_shape(World) +
  tm_borders() +
  tm_fill("HPI", style = "quantile", palette = "Greens", title = "Human Poverty Index (HPI)") +
  tm_text("name", size = 0.2, col = "black", root = 3) +
  tm_layout(legend.position = c("left", "bottom"), 
            legend.title.size = 0.8,  
            legend.text.size = 0.5)   


However, the map can be modified to add better visualizations (easier on the eyes)! In this example, in order to remove the labels off the map so that there is no overlap, tmap can be converted to a leaflet map. Instead of label overlap, there are popups of the country’s abbreviations that will pop up when the user hovers their cursor over the country on the map. The leaflet allows for various tiles to be added as well. These are small, square images or pieces of a larger map that are loaded and displayed on a digital map interface that cover geographical areas and allow for zooming and panning.

library(tmap)
library(leaflet)

tm <- tm_shape(World) +
  tm_borders() +
  tm_fill("HPI", style = "quantile", palette = "Greens", title = "Human Poverty Index (HPI)") +
  tm_text("name", size = 0.2, col = "black", root = 3) +
  tm_layout(legend.position = c("left", "bottom"), 
            legend.title.size = 0.8,  
            legend.text.size = 0.5)   


leaflet_map <- tmap_leaflet(tm)

tmap_mode("view")
tmap_leaflet(tm)


Another component that users can add to their maps is text markers with details or titles of their choice to mark down a specific location. On this map, some cities were pinpointed on the map using the latitude and longitude. This can be customized to present whatever information users would like to showcase.

library(tmap)
library(leaflet)

tm <- tm_shape(World) +
  tm_borders() +
  tm_fill("HPI", style = "quantile", palette = "Greens", title = "Human Poverty Index (HPI)") +
  tm_text("name", size = 0.2, col = "black", root = 3) +
  tm_layout(legend.position = c("left", "bottom"), 
            legend.title.size = 0.8,  
            legend.text.size = 0.5)

cities <- data.frame(
  name = c("New York", "Paris", "Tokyo", "Sydney"),
  lon = c(-74.006, 2.3522, 139.6917, 151.2093),
  lat = c(40.7128, 48.8566, 35.6895, -33.8688)
)


leaflet_map <- tmap_leaflet(tm)

leaflet_map <- leaflet_map %>%
  addPopups(
    lng = cities$lon,
    lat = cities$lat,
    popup = cities$name,
    options = popupOptions(closeButton = FALSE)
  )

leaflet_map


Conclusion

This code-through was a very basic example of tmap, some of its functions, and how it can be used in conjunction with other packages to showcase data in the form of maps. tmap is a powerful R package for creating thematic maps and it simplifies the creation of both static and interactive maps allowing users to visualize spatial data. Integrating it with other packages like leaflet, in the above example, allows for interactive maps with pop-ups and diverse layers. Overall, tmap is a valuable tool for spatial data visualization in R, providing a straightforward yet versatile approach to map creation and exploration.



Works Cited

This code through references and cites the following sources: