Authors

Andree Valle Campos

Laure Vancauwenberghe

Kene David Nwosu

Published

December 17, 2024


1 Introduction

Country borders or boundaries can have several usages. For example, they can be used as background in Thematic maps or as delimiters of other Spatial data to ease the identification of spread patterns.

An example of the former is shown in Figure 1, where we retrieve the intersection between two spatial objects: points within polygons.

Figure 1. The st_intersection() function creates a new geometry with the shared portion of x and y.

However, the access to this type of data can have different outputs, for example, the low or high resolution of continent and country borders, or the availability of certain administrative levels. The choice of these outputs will depend of your needs!

In this lesson we are going to learn how to access continent, country and administrative level borders using {rnaturalearth}, {rgeoboundaries}, and {geodata} packages.


2 Learning objectives

  1. Access to low resolution continent and country borders with {rnaturalearth}

  2. Access to high resolution country and administrative level borders with {rgeoboundaries}


3 Prerequisites

This lesson requires the following packages:

Code
if(!require('pacman')) install.packages('pacman')

pacman::p_load(rnaturalearth,
               malariaAtlas,
               ggplot2,
               cholera,
               geodata,
               here,
               sf, 
               rgeoboundaries)

pacman::p_load_gh("afrimapr/afrilearndata")

4 Mapping country borders with {rnaturalearth}

• Let’s draw a world map with country borders.

{rnaturalearth} can map all the countries in the world, among others.

• Use ne_countries() with the returnclass = "sf" argument.

Code
# YOUR CODE HERE
countries <- ne_countries(returnclass = "sf")

class(countries)
[1] "sf"         "data.frame"

• It returns an sf object with the shapes for all countries!

• So, countries can be plotted with geom_sf():

Code
# YOUR CODE HERE
ggplot(data = countries) +
  geom_sf()

• Wonderful!

4.1 A single continent

• Let’s subset the "south america" continent, using the continent argument of ne_countries():

Code
# Countries in South America
south_am <- ne_countries(returnclass = "sf",
                         continent = "south america") # 👈👈👈👈

ggplot(data = south_am) + 
  geom_sf()

continent can accept multiple continents

• Let’s try "north america" and "south america":

Code
# Countries in north and south america
north_south_am <- ne_countries(returnclass = "sf",
                               continent = c("north america", "south america")) # 👈👈👈👈

ggplot(data = north_south_am) +
  geom_sf()

◘ Use ne_countries(), ggplot() and geom_sf() to plot a single map of all the countries in the Asia and Africa continent

Code
q_asia_africa <- 
  ne_countries(returnclass = "sf", 
            continent = c("asia", "africa"))

ggplot(data = q_asia_africa) +
  geom_sf()

4.2 Multiple countries

• Subset one or multiple countries, e.g. "nigeria" and "niger"

• Use the country argument:

Code
# Map of Nigeria and Niger
nigeria_niger <- ne_countries(returnclass = "sf", country = c("nigeria", "niger"))# CONTINUE THE CODE

ggplot(data = nigeria_niger) +
  geom_sf()

◘ Use ne_countries(), ggplot() and geom_sf() to plot a single map of the national borders of China and Indonesia

Code
china_indonesia <- 
  ne_countries(returnclass = "sf", 
            country = c("china", "indonesia"))

ggplot(data = china_indonesia) +
  geom_sf()

5 Mapping country borders with {rgeoboundaries}

{rnaturalearth} access borders that do not need too much boundary resolution.

{rgeoboundaries} access to the high resolution country boundaries.

Figure 2. Ireland according to {rnaturalearth} and {rgeoboundaries} packages.

{rgeoboundaries} is a client for the geoBoundaries API,

• It provides country political administrative boundaries.

5.1 A single country

• Use geoboundaries() to download the administrative boundary of "Zimbabwe".

Code
zimbabwe_boundary <- geoboundaries(country = "zimbabwe")

zimbabwe_boundary is a "sf" class object.

• So, zimbabwe_boundary can be plotted with geom_sf():

Code
ggplot(data = zimbabwe_boundary) +
  geom_sf()

Download the boundaries of Sierra Leone using the geoboundaries() function.

Code
q1 <- geoboundaries(country = "Sierra Leone")

ggplot(q1) +
  geom_sf()

5.2 Different administrative levels

• If available, we can also download lower levels of administrative boundaries.

• Let’s pass the administrative level to geoboundaries().

• Administrative level 1 (1) is the highest level,

• Administrative level 5 (5) is the lowest.

• Let’s get the first (1) administrative level boundaries of "Zimbabwe":

Code
zimbabwe_boundaries_adm1 <- geoboundaries(country = "zimbabwe", adm_lvl = 1)

ggplot(data = zimbabwe_boundaries_adm1) +
  geom_sf()

• Let’s get the second (2) administrative level boundaries of Zimbabwe:

Code
zimbabwe_boundaries_adm2 <- geoboundaries(country = "zimbabwe",
                                   adm_lvl =  2)

ggplot(data = zimbabwe_boundaries_adm2) +
  geom_sf()

• Countries could be further sub-divided into administrative divisions from 1 to 5.

Download the third administrative level boundaries of Sierra Leone, using the geoboundaries() function.

Code
SL_boundaries_adm3 <- geoboundaries(country = "Sierra Leone",
                                   adm_lvl =  3)

ggplot(data = SL_boundaries_adm3) +
  geom_sf()

• Let’s download boundaries of multiple countries together

• Include their names as a vector: c("country_01","country_02").

second administrative level boundaries of adjacent countries: Zimbabwe and Mozambique

Code
zimbabwe_mozambique_adm2 <- 
  geoboundaries(country = c("zimbabwe","Mozambique"),
                adm_lvl  = 2)
Code
ggplot(data = zimbabwe_mozambique_adm2) +
  geom_sf()

6 Wrap up

• How to access low and high resolution continent, country and multiple administrative level borders

• Using {rnaturalearth} and {rgeoboundaries}.


Contributors

The following team members contributed to this lesson:


References

Some material in this lesson was adapted from the following sources:

This work is licensed under the Creative Commons Attribution Share Alike license. Creative Commons License


Answer Key

Practice Q

Use ne_countries(), ggplot() and geom_sf() to plot a single map of all the countries in the Asia and Africa continent

Code
q_asia_africa <- 
  ne_countries(returnclass = "sf", 
                continent = c("asia", "africa")) 

ggplot(data = q_asia_africa) +
  geom_sf()

Practice Q

Use ne_countries(), ggplot() and geom_sf() to plot a single map of the national borders of China and Indonesia

Code
china_indonesia <- 
  ne_countries(returnclass = "sf",
               country = c("china", "indonesia"))

ggplot(data = china_indonesia) +
  geom_sf()

Practice Q

Download the boundaries of Sierra Leone using the geoboundaries() function.

Code
q1 <- geoboundaries(country = "Sierra Leone")
q1

Practice Q

Download the third administrative level boundaries of Sierra Leone, using the geoboundaries() function.

Code
q2 <- geoboundaries(country = "Sierra Leone", adm_lvl = 3)
q2