The “One Minute Map” — static maps creation with the R tmap package: The case of MetaWealth Index in Nigeria

Hany IMAM
4 min readJan 25, 2022

--

Yes, you have read it right! This post is not related to Missy or Luda famous “One Minute Man” song, yet it is a straightforward introduction to a series of data analysis applications in R, particularly how to create and deal with maps in R and Rstudio within a minute!!

This is the first of a series that I will be sharing in Medium for data scientists and lovers to put more hands on mapping using R.

In this session, I will take you through the smooth journey of creating static maps with the “tmap” package in R in less than a minute.

About the data

As a humanitarian data scientist, I focus on areas where conflictivity, poverty and fragility are the main drivers of my research, hence I am using Nigeria as an example to map the “Weighted Relative Wealth Index” that was produced by Meta Data for Good team with a big dataset containing more than 100K observations (records). The dataset has only 4 variables, longitude, latitude, wri and error. More on the data and the study behind it is well explained by Meta here.

The static map

Using the “tmap” package in R, I was able to create the first map of the WRI using the dots technique.

To create a dot map, you need a dataset with longitude and latitude and some indicators to add more information to your map, or you need a shapefile that has all the information embedded within its relative components.

In “tmap” package, you convert the dataset to a geographic standard file using the st_as_sf, by setting the coordinates variables and teh CRS projection system:

wrimap <- st_as_sf(nga_wealth_index, coords = c(“longitude”, “latitude”), crs = 4326)

I then used tm_shape and tm_dots to inform the console that I am creating a dots map:

tm_shape(wrimap) +
tm_dots(alpha = 0.8, col = “rwi”, palette = “PRGn”,
stretch.palette = TRUE, size = 0.01)+

The layout is an optional item in “tmap” where a user adjusts the visualization parameters:
tm_layout (frame = TRUE, bg.color = “transparent”,
legend.title.size = 1,
legend.text.size = 0.6,
legend.position = c(“right”,”bottom”),
legend.bg.color = “white”,
legend.bg.alpha = 1,
main.title = “Weighted Relative Wealth Index in Nigeria”,
main.title.size = 1
)

Here is the result map showing the weighted relative wealth index in Nigeria.

Deep dive into the RWI data

The map shows a significant difference in wealth between the northern part of the country where it tends to have a very low negative index, and the southern part where it looks wealthier as per the index.

Again, as a humanitarian data scientist, I am more concerned in areas where conflict might be a driver of a less wealthier context.

In this regard, I will be mapping the average RWI on the Local Government Areas (LGA) level for the Borno, Adamawa and Yobe (BAY) states.

The shapefile of the LGAs can be downloaded from the Humanitarian Data Exchange (HDX) website.

Using the “point.in.poly” function from the “SpatialEco” package, I got the LGAs information added to my points shapefile. I then calculated the mean of WRI in each o the LGAs by using the“dplyr” package.

“Tigris” package is then requested to join back the new dataframe with the WRI mean to the LGAs shapefile:

lga_rwi <- geo_join(adm2,
wri_mean,
“ADM2_EN”,
“ADM2_EN”,
how = “left”) # Joins the rwi average to the LGA shapefile

Now it is the time to map our RWI data over the LGAs, using the tm_fill function from the “tmap” package:

wri.lga.map <-
tm_shape(lga_rwi) +
tm_fill(col = “wri”,
palette = “PRGn”,
stretch.palette = FALSE,
n = 10) +
tm_borders(col = “black”,
lwd = 0.5) +
tm_text(“ADM2_EN”,
size = 0.7) +
tm_layout(
main.title = “Average Wealth Index in BAY States”,
main.title.size = 1,
legend.position = c(“left”, “bottom”),
legend.text.size = 1,
legend.text.color = “black”,
legend.title.size = 1,
bg.color = “#d0e2ec”,
legend.outside = T
)
The results are shown in the map below.

As you may see, there is a tendency to less wealth in the LGAs of the three states except for the two main cities Maiduguri and Yola where the WRI is reaching a positive number between 0.8 and 1.2 which is also considered to be relatively low.

In the next sessions, we will do some spatial correlation analysis with different datasets that might allow us revealing the reason behind a low RWI in such context.

--

--