library(tidyverse)
library(tmap)
library(leaflet)
library(dplyr)
library(sf)
# Install openwashdata packages by uncommenting the following lines
# install.packages("devtools")
# devtools::install_github("openwashdata/cbssuitabilityhaiti")
# devtools::install_github("openwashdata/waterpumpkwale")
library(cbssuitabilityhaiti)
library(waterpumpkwale)
Review
Welcome to the final post in our series on creating and visualizing WASH data maps. In this post, we’ll focus on crafting an interactive map that enhances user experience and data exploration. If you missed the previous posts or need a refresher, you can revisit them here:
Introduction
Why an interactive map?
Interactive maps allow users to explore data more deeply by offering the ability to zoom in and out for different perspectives. They also make it easy to analyze various groups within the map, such as showing or hiding specific layers (e.g., water pumps of different types) to focus on particular variables. Additionally, interactive maps can include detailed regional information—like GDP or population—which can be revealed through hover effects, enriching the overall understanding of the data.
Useful R libraries
In the previous post, we explored some useful R libraries such as sf
and tmap
. We will explore more with tmap
again, and we introduce a commonly used library for plotting interactive maps, leaflet
.
leaflet
: an open-source JavaScript library for interactive maps.
Useful openwashdata datasets
Many openwashdata datasets use interactive maps in their demos. Here we will showcase the following data packages:
cbssuitabilityhaiti
: Spatial data to support an analysis of suitability of container-based sanitation in flood prone areas of Haiti.waterpumpkwale
: Weekly volume of water pumped for handpumps monitored with Smart Handpump technology, Kwale County, Kenya
Plot interactive maps
Installing and Loading Required Packages
Before we dive in, ensure you have the necessary packages installed and loaded:
Plotting maps with tmap
library
In this section, we’ll learn how to create interactive maps using the tmap
library in R. To do this, we’ll work with a data package called cbssuitabilityhaiti
, which includes two datasets: mwater
and okap
.
- The
mwater
dataset contains information about different types of water access points in Haiti, such as boreholes and protected springs. - The
okap
dataset provides information on population density, socioeconomic status, suitability for pit latrines, and areas where sewage construction is prioritized.
To understand the context, Haiti is a island state in the Caribbean sea. It has four administrative levels where the 3rd level is used for this tutorial. The 3rd administrative level contains 134 areas. Our focus region is the extended area around Cap Haïtien and l’Acul-du-Nord.
We’ll use the tmap library in R to visualize maps showing water access points and their distribution across regions with different socioeconomic statuses.
Step 1: Downloading and Unzipping External Data
First, we need to obtain spatial data that outlines the boundaries of Haiti’s 3rd administrative level areas. We’ll download this data from the Stanford database and unzip it to use in our analysis. The following code will download the files to your current working directory.
# Download the zip file containing Haiti ADM3 spatial data
::download.file(url = "https://stacks.stanford.edu/file/druid:kz179wf4778/data.zip?download=true", destfile = "3rd-level-haiti-divisions.zip", mode = "wb")
utils
# Unzip the downloaded file into current working directory
unzip("3rd-level-haiti-divisions.zip", exdir = "haiti-adm3")
# Optionally, remove the zip file to clean up your workspace
file.remove("3rd-level-haiti-divisions.zip")
#> [1] TRUE
Step 2: Reading the Spatial Data
Once the data is unzipped, we need to read the spatial files into R so we can work with them. We’ll use the sf
package, which is great for handling spatial (geographic) data in R.
# Read the shapefile of Haiti's ADM3 into an sf object
<-
haiti_adm3 st_read("haiti-adm3/HTI_adm3.shp") |>
st_as_sf()
#> Reading layer `HTI_adm3' from data source
#> `/Users/lschoebitz/Documents/gitrepos/gh-org-openwashdata/website/pages/blog/posts/2024-03-01-create-map-part-3/haiti-adm3/HTI_adm3.shp'
#> using driver `ESRI Shapefile'
#> Simple feature collection with 134 features and 15 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -74.48125 ymin: 18.0218 xmax: -71.61815 ymax: 20.09042
#> Geodetic CRS: WGS 84
# Set tmap to interactive view mode
tmap_mode("view")
#> tmap mode set to interactive viewing
We are particularly interested in the northern part of Haiti, focusing on the regions around Cap-Haïtien and l’Acul-du-Nord. These regions correspond to ID_2 = 24
and ID_2 = 25
in the haiti_adm3
dataset.
The code below creates an interactive map where:
- All regions are shown in orange.
- Cap-Haïtien and l’Acul-du-Nord are highlighted in yellow.
You can explore the map interactively, clicking on regions to see more information.
# Select Cap Haïtien and l'Acul-du-Nord
<- filter(haiti_adm3, (ID_2 == 24 | ID_2 == 25) & ID_3 != 76 & ID_3 != 80)
caphaitien
|>
haiti_adm3 tm_shape() +
tmap_options(check.and.fix = TRUE) +
tm_borders() +
tm_fill(col = "orange", alpha = 0.6) +
tm_shape(caphaitien) +
tm_borders() +
tm_fill(col = "yellow", alpha = 0.6)
#> Warning: The shape haiti_adm3 is invalid. See sf::st_is_valid