In this tutorial, we will walk through the steps to create a choropleth map visualizing Ethiopian regions. Choropleth maps are useful for representing spatial variations across different regions.

Step 1: Setting Up

First, make sure you’ve installed the necessary R packages:

library(ggplot2)
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(leaflet)

Step 2: Load the Data

# Download and unzip the GeoJSON file
url <- "https://geodata.ucdavis.edu/gadm/gadm4.1/json/gadm41_ETH_1.json.zip"
download.file(url, destfile = "gadm41_ETH_1.json.zip")
unzip("gadm41_ETH_1.json.zip", exdir = "ethiopian_data")

# Read the GeoJSON file using sf
ethiopian_data <- st_read("ethiopian_data/gadm41_ETH_1.json")
## Reading layer `gadm41_ETH_1' from data source 
##   `/home/kgezaheg@macalester.edu/Comp112/ethiopian_data/gadm41_ETH_1.json' 
##   using driver `GeoJSON'
## Simple feature collection with 11 features and 11 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 33.0015 ymin: 3.3988 xmax: 47.9582 ymax: 14.8455
## Geodetic CRS:  WGS 84
# Check the structure
str(ethiopian_data)
## Classes 'sf' and 'data.frame':   11 obs. of  12 variables:
##  $ GID_1    : chr  "ETH.1_1" "ETH.2_1" "ETH.3_1" "ETH.4_1" ...
##  $ GID_0    : chr  "ETH" "ETH" "ETH" "ETH" ...
##  $ COUNTRY  : chr  "Ethiopia" "Ethiopia" "Ethiopia" "Ethiopia" ...
##  $ NAME_1   : chr  "AddisAbeba" "Afar" "Amhara" "Benshangul-Gumaz" ...
##  $ VARNAME_1: chr  "ĀddīsĀbaba|AddisAbaba|Adis-Abe" "NA" "Amara" "BeneshangulGumu" ...
##  $ NL_NAME_1: chr  "NA" "NA" "NA" "NA" ...
##  $ TYPE_1   : chr  "Astedader" "Kilil" "Kilil" "Kilil" ...
##  $ ENGTYPE_1: chr  "City" "State" "State" "State" ...
##  $ CC_1     : chr  "14" "02" "03" "06" ...
##  $ HASC_1   : chr  "ET.AA" "ET.AF" "ET.AM" "ET.BE" ...
##  $ ISO_1    : chr  "NA" "ET-AF" "NA" "NA" ...
##  $ geometry :sfc_MULTIPOLYGON of length 11; first list element: List of 1
##   ..$ :List of 1
##   .. ..$ : num [1:89, 1:2] 38.9 38.9 38.9 38.9 38.9 ...
##   ..- 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:11] "GID_1" "GID_0" "COUNTRY" "NAME_1" ...

Step 3: Prepare the Color Palette

# Extract unique values from the 'NAME_1' column of the 'ethiopian_data' dataset
unique_names <- unique(ethiopian_data$NAME_1)

# Create a color palette function 'pal' using the 'colorFactor' function
# This palette assigns colors to the unique region names ('unique_names')
# The colors are chosen from the 'Blues' color palette
pal <- colorFactor(palette = "Blues", domain = unique_names)

Step 4: Create the Map

Ethiopia’s Adminstrative Regions

# Initialize a Leaflet map using the Ethiopian data
leaflet(ethiopian_data) %>%
  # Add default map tiles (e.g., OpenStreetMap)
  addTiles() %>%
  # Add polygons to the map, representing regions in Ethiopia
  addPolygons(
    fillColor = ~pal(NAME_1), # Set the fill color of the polygons based on 'NAME_1' using a color palette function 'pal'
    weight = 2,               # Set the border weight of the polygons
    opacity = 1,              # Set the border opacity of the polygons
    color = "white",          # Set the border color of the polygons
    dashArray = "3",          # Set the style of the border to be dashed
    fillOpacity = 0.7,        # Set the fill opacity of the polygons
    highlight = highlightOptions(
      weight = 5,             # Set the weight of the border for highlighted polygons
      color = "#666",         # Set the color of the border for highlighted polygons
      dashArray = "",         # Remove the dash style for highlighted polygons
      fillOpacity = 0.7,      # Set the fill opacity for highlighted polygons
      bringToFront = TRUE     # Bring the highlighted polygons to the front
    ),
    label = ~as.character(NAME_1) # Add labels to the polygons, using the 'NAME_1' field
  ) %>% 
  # Add a legend to the map
  addLegend(
    pal = pal,               # Define the color palette for the legend
    values = unique_names,   # Define the values for the legend based on unique region names
    title = "Ethiopia's Regions", # Title for the legend
    position = "topright"    # Position of the legend on the map
  ) %>%
  # Add a custom control for the caption
  addControl(
    html = "<p style='margin: 4px; font-size:12px;'>Data Source: UC Davis</p>", # HTML content for the caption
    position = "bottomleft", # Position of the caption on the map
    className = "my-caption" # Custom CSS class for the caption
  )
## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Blues is 9
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(max(3, n), palette): n too large, allowed maximum for palette Blues is 9
## Returning the palette you asked for with that many colors

Conclusion:

This tutorial walked you through creating a choropleth map in R using the leaflet package. With this foundation, you can further customize and enhance your maps, integrate other datasets, or explore additional features of the leaflet package.