10. Mapping With Natural Earth
“In the beginning there was nothing, which exploded.”
— Terry Pratchett
“Biology is the study of complicated things that have the appearance of having been designed with a purpose.”
— Richard Dawkins
Web resources about R for Spatial Applications
Now that we are upgrading to better, more powerful maps, you’ll need to refer to industrial-strength documentation for detailed help. Please refer to links below for information about the vast array of functions available for spatial computations and graphics.
AUTHOR | TITLE |
---|---|
Spatial R | |
Edzer Pebesma | Simple Features for R |
Edzer Pebesma, Roger Bivand | Spatial Data Science with applications in R |
Robin Lovelace et al. | Geocomputation with R |
Manuel Gimond | Intro to GIS and Spatial Analysis |
Wasser et al. | Introduction to Geospatial Raster and Vector Data with R |
Taro Mieno | R as GIS for Economists |
The sf package
The sf package in R is a package for handling and processing spatial data. In recent years it has become the de facto package to use for many mapping application, replacing older packages such as sp and including the C libraries GEOS 3, GDAL, and PROJ. It provides classes for storing and manipulating simple feature geometries, and functions for working with spatial data. ‘Simple features’ refer to a standardised way of encoding vector data, including points, lines, and polygons, that are widely used in geographic information systems (GIS).
The sf package was created to provide a fast and efficient way to work with vector data in R, and it is designed to integrate with other packages in the tidyverse, such as dplyr and ggplot2, allowing for seamless processing and visualisation of spatial data. The package provides a variety of functions for data import, transformation, manipulation, and analysis, making it a valuable tool for working with spatial data in R.
In addition to its core functionality, the sf package also provides a set of methods for converting between different data representations, such as data frames, matrices, and lists, making it a versatile tool for working with spatial data in a variety of formats.
While sf works with vector data, raster data require the well-known but old raster package, or its modern replacements terra and stars. I will not work with raster data in this Chapter.
Maps with rnaturalearth
Natural Earth is a public domain map dataset that provides high-quality, general-purpose base maps for the world at various scales. It was designed to be a visually pleasing alternative to other public domain datasets, and its creators aim to provide the data in a form that is useful for a wide range of applications and to make it easy to use and integrate with other data.
The dataset includes a variety of geographic features, including coastlines, rivers, lakes, and political boundaries, as well as cultural features like cities, roads, and railways. The data are available in several different formats, including vector and raster, and it can be used with a variety of software, including GIS and mapping applications. Within R we can access these map layers using the rnaturalearth package.
One of the key benefits of Natural Earth is its public domain status, which means that anyone can use and distribute the data without restrictions or licensing fees. This makes it an ideal choice for individuals who need high-quality base maps for their projects but may not have the resources or expertise to create them from scratch. I am not convinced that students actually read this. The first person to send me a WhatsApp mentioning the phrase “Know your maps” will get a Lindt chocolate.
In addition to its public domain status, Natural Earth is also regularly updated with new data to ensure that the maps remain accurate and up-to-date. This makes it a valuable resource for anyone who needs reliable and up-to-date geographic data.
Install packages and set things up
First, I define the extent of the map region:
# the full map extent:
xmin <- 12; ymin <- -36.5; xmax <- 40.5; ymax <- -10
xlim <- c(xmin, xmax); ylim <- c(ymin, ymax)
# make a bounding box for cropping:
bbox <- st_bbox(c(xmin = xmin, ymin = ymin,
xmax = xmax, ymax = ymax))
# might be useful for zooming into a smaller region (False Bay and
# the Cape Peninsula):
xlim_zoom <- c(17.8, 19); ylim_zoom <- c(-34.5, -33.2)
Load the data and make maps
Let us see what is inside the safrica_countries
object:
As you can see, it is a data.frame
and tbl
(tibble), amongst other classes, and so you can apply many of the tidyverse functions to it, including select()
, filter()
, summarise()
and so on. The class()
argument additionally indicates that it has some simple features properties, so some functions provided by the sf package also becomes available to use. You can see some of these functions in action, below.
sf
class
sf
indicates that the object is of class simple features. In sf
language, what would be called columns (variables) in normal tidyverse speak becomes known as attributes—these are the properties of the map features, with the features being the types of geometrical representations of geographical objects.
Let us plot the entire safrica_countries
object to see all the attributes of all of the features. This kind of figure a called a choropleth map:
You probably don’t want to plot all of them. Let us select one:
You might achieve the same in a more familiar way:
Or you may want to plot the estimate of the population size, which is contained in the attribute pop_est
:
The names of the countries are in the rows down the safrica_countries
object, and so they become accessible with filter()
. Let us only plot some attribute for South Africa:
You can continue to add additional operations to create a new map:
So far you have relied on the base R plot function made for the simple features. You can also plot the map in ggplot using a more familiar and more customisable interface:
Now you can layer another feature:
Example
Here are examples that use the built-in Fiji earthquake data or the Kaggle earthquake data.
Reuse
Citation
@online{smit,_a._j.2021,
author = {Smit, A. J.,},
title = {10. {Mapping} {With} {Natural} {Earth}},
date = {2021-01-01},
url = {http://tangledbank.netlify.app/BCB744/intro_r/10-mapping_rnaturalearth.html},
langid = {en}
}