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.
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:

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}

As an introduction to plotting boundaries in R, let’s look at how to draw a simple world map with country borders. The package {rnaturalearth} contains information to map all the countries in the world, among other things.

To obtain this map information, use the ne_countries() function, with the argument returnclass = "sf".

countries <- ne_countries(returnclass = "sf")

The code returns an sf object with the shapes for all countries.

Now, the countries object can be plotted very easily with the geom_sf() function of {ggplot2}:

ggplot(data = countries) + 
  geom_sf()

Wonderful! (Almost too easy!)

4.1 A single continent

To subset to a specific continent, use the continent argument of ne_countries():

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

ggplot(data = south_am) + 
  geom_sf()

The continent argument can accept a vector with multiple continents:

# 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

asia_africa <- 
  .........(returnclass = "sf", 
            ......... = c(".........", "........."))

ggplot(data = asia_africa) +
  geom_sf()

4.2 Multiple countries

To subset to a specific country or specific countries, use the country argument:

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

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

china_indonesia <- 
  .........(returnclass = "sf", 
            ......... = c(".........", "........."))

ggplot(data = china_indonesia) +
  geom_sf()

5 Mapping country borders with {rgeoboundaries}

{rnaturalearth} is useful for accessing continents and country borders that do not need much boundary resolution. {rgeoboundaries} is an alternative package that provides access to high resolution country boundaries.

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

The {rgeoboundaries} package is a client for the geoBoundaries API, providing country political administrative boundaries.

5.1 A single country

To download boundaries of countries we use the geoboundaries() function of {rgeoboundaries}. For example, we can download the administrative boundary of Zimbabwe and assign it to a variable called zimbabwe_boundary as follows.

zimbabwe_boundary <- geoboundaries(country = "Zimbabwe")
  • The zimbabwe_boundary is a "sf" class object.

  • {ggplot2} allows us to easily visualise simple feature objects using the geom_sf() function.

  • It can be used to plot the administrative boundary of Zimbabwe as follows:

ggplot(data = zimbabwe_boundary) +
  geom_sf()

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

q1 <- ________(________ = "Sierra Leone")
q1

5.2 Different administrative levels

If available, lower levels of administrative boundaries in countries can be downloaded too. We just have to pass the administrative level as an argument in the geoboundaries() function.

Administrative level 1 (1) is the highest level, while administrative level 5 (5) is the lowest. This means the country will be further sub-divided into administrative divisions as the Administrative level progresses from 1 to 5.

See how the first and second administrative level boundaries of Zimbabwe are downloaded below.

# downloading administrative level 1 boundaries
zimbabwe_boundaries_adm1 <- geoboundaries(country = "Zimbabwe",
                                         adm_lvl = 1)

ggplot(data = zimbabwe_boundaries_adm1) +
  geom_sf()

# downloading administrative level 2 boundaries
zimbabwe_boundaries_adm2 <- geoboundaries(country = "Zimbabwe",
                                         adm_lvl = 2)

ggplot(data = zimbabwe_boundaries_adm2) +
  geom_sf()

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

q2 <- geoboundaries(country = "Sierra Leone", ________ = ________)
q2

We can also download the boundaries of multiple countries together by including the names of countries as a vector class object like: c("country_01","country_02").

See how the second administrative level boundaries of adjacent countries like Zimbabwe and Mozambique are downloaded and plotted below.

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

6 Wrap up

In this lesson, we have learned 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