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.
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.
Access to low resolution continent and country borders
with {rnaturalearth}
Access to high resolution country and administrative
level borders with {rgeoboundaries}
This lesson requires the following packages:
{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"
.
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}
:
Wonderful! (Almost too easy!)
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:
{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.
The {rgeoboundaries}
package is a client for the geoBoundaries API, providing
country political administrative boundaries.
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.
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:
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.
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.
In this lesson, we have learned how to access
low and high resolution continent, country and
multiple administrative level borders using {rnaturalearth}
and {rgeoboundaries}
.
The following team members contributed to this lesson:
Some material in this lesson was adapted from the following sources:
Seimon, Dilinie. Administrative Boundaries. (2021). Retrieved 15 April 2022, from https://rspatialdata.github.io/admin_boundaries.html
Varsha Ujjinni Vijay Kumar. Malaria. (2021). Retrieved 15 April 2022, from https://rspatialdata.github.io/malaria.html
Batra, Neale, et al. The Epidemiologist R Handbook. Chapter 28: GIS Basics. (2021). Retrieved 01 April 2022, from https://epirhandbook.com/en/gis-basics.html
Lovelace, R., Nowosad, J., & Muenchow, J. Geocomputation with R. Chapter 2: Geographic data in R. (2019). Retrieved 01 April 2022, from https://geocompr.robinlovelace.net/spatial-class.html
Moraga, Paula. Geospatial Health Data: Modeling and Visualization with R-INLA and Shiny. Chapter 2: Spatial data and R packages for mapping. (2019). Retrieved 01 April 2022, from https://www.paulamoraga.com/book-geospatial/sec-spatialdataandCRS.html
This work is licensed under the Creative Commons Attribution Share Alike license.