Most times spatial data requires geographic context to locate events with environmental features like rivers or streets.
In this lesson, we are going to learn about how to add Physical features to our maps and plot them in multiple layers to complement Thematic maps!
Create Physical feature maps to visualize roads or rivers.
Complement Thematic maps with Physical features as background.
Relate Physical features with a Geometry type.
This lesson requires the following packages:
if(!require('pacman')) install.packages('pacman')
pacman::p_load_gh("afrimapr/afrilearndata")
pacman::p_load(ggspatial,
ggplot2,
tibble,
mdsr,
terra,
spData,
sf,
readr)
This lesson requires familiarity with {ggplot2}
: if you
need to brush up, have a look at our introductory course on data
visualization.
Physical features include roads, buildings and rivers.
Using {ggplot2}
, geom_sf()
, and the
color
argument.
We can create a Physical feature map of the Road network in South
America and color
them according to their type:
south_am_roads <-
read_rds(here("data/south_am_roads.rds"))
ggplot(data = south_am_roads) +
geom_sf(aes(color = type))
Let’s create a map of the Sacramento basin in California US
(sacramento_rivers
), colored by their feature type
(FTYPE
).
sacramento_rivers <-
read_rds(here("data/sacramento_rivers.rds"))
ggplot(data = sacramento_rivers) +
geom_sf(aes(_____ = FTYPE), size = 1)
Data from here: https://zenodo.org/record/4985219
In Environmental Epidemiology, we can map numeric variables like the concentration of chemicals or pollutants in wastewater surveillance, or in Ecology, richness estimations from rivers.
Below is an example mapping a classification of rivers in the Sacramento basin in California US.
sacramento_rivers <-
read_rds(here("data/sacramento_rivers.rds"))
ggplot(data = sacramento_rivers) +
geom_sf(mapping = aes(color = richness))
“Richness” is the estimated number of fish species in each segment of the river.
We can use Trajectory data and Road network data to evaluate the effects of human movement patterns in infectious disease transmission like Dengue or Malaria.
Both of them are depicted in a map using Lines.
Each Thematic map has its respective type of Geometry.
So far we have been plotting single geometries. But for most maps, you will need to provide additional context. This can be done by plotting multiple geometries.
We can complement Thematic maps with spatial Physical features like roads, buildings and rivers.
As an example, we will complement a Choropleth map with the
population (pop_est
) of African countries, from the
africountries
, with:
afrihighway
dataset from the same package.ggplot() +
geom_sf(data = africountries, mapping = aes(fill = pop_est)) +
geom_sf(data = afrihighway)
Here, the physical feature afrihighway
is
above all the other layers.
But it can also be below. For example, we can
complement a Dot map with the population in the capital cities of
Africa, from the africapitals
dataset, with the same
layer:
ggplot() +
geom_sf(data = afrihighway) +
geom_sf(data = africapitals, mapping = aes(size = pop, color = pop))
This is how you plot another map layer on top of another map.
{ggplot2}
allows to overlap multiple layers (of
maps) from different data sources to complement Thematic
maps.
For this, instead of a global specification of data, you need a local specification of data:
## Error in ggplot(data = data_global): object 'data_global' not found
## Error in fortify(data): object 'data_local_layer_1' not found
The order of the layers (below or above) will depend on the aim of the plot.
Create a multiple layer Thematic map with:
the world
dataset (from the {spData}
package).
Then, overlap it with the African trans-continental highway
network lines from the afrihighway
dataset.
Use the geom_sf()
function for each layer:
In this example, we replicate John Snow’s Dot map with the locations of deaths of the 1854 London cholera outbreak. We complemented this map with a physical feature like the street roads of the city: subfigure B is much more readable than subfigure A right?!
In Figure 3B, the physical feature is used as background, below the Dot map. This is why it looks much more readable.
In this lesson, we have learned about how to represent Physical features and how to use them to complement Thematic maps in multiple layers.
However, most times spatial data requires more explicit Google Maps-like geographic context! For this you will need to learn how to add Basemaps for Google Maps-like backgrounds. Follow along with the lessons to find how to do this!
The following team members contributed to this lesson:
Some material in this lesson was adapted from the following sources:
Batra, Neale, et al. (2021). 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
Baumer, Benjamin S., Kaplan, Daniel T., and Horton, Nicholas J. Modern Data Science with R. Chapter 17: Working with geospatial data. (2021). Retrieved 05 June 2022, from https://mdsr-book.github.io/mdsr2e/ch-spatial.html
This work is licensed under the Creative Commons Attribution Share Alike license.