• sf stands for “simple features”, an open standard used to represent a wide range of geometric shapes.
Create a Choropleth map with the world data from the {spData} package, using geom_sf(), to portrait its countries and fill them in relation to its population, available in the pop variable.
Code
library(spData)# Write and visualize your answer:q1 <- world %>%ggplot() +geom_sf(mapping =aes(fill = pop))
With Categorical data
• Let’s keep using fill.
• Create a map with countries colored by their Economical classification (economy):
Code
countries <- rnaturalearth::ne_countries(returnclass ="sf")ggplot(data = countries) +geom_sf(mapping =aes(fill = economy))
• Before using geom_sf(), verify that your Spatial data is an "sf" R object:
Create a Thematic map with the afriairports object to portrait all its airport locations, using geom_sf(), and color them in relation to the type variable.
Code
# Write and visualize your answer:q2 <-ggplot(data = afriairports) +geom_sf(mapping =aes(color = type))
How to use it?
• To visualize the scatter of your data and visually scan for clusters.
Figure 3. Dot map. Location of simulated Ebola cases in Sierra Leone, colored by each case outcome.
• Dot maps visualize a shape called Point.
• Collects data that register the locations of random events.
• E.g., geographical coordinates of individuals with a given diagnosis (Figure 3).
• Bothered by having just dots and no geographical context?
• That’s good! We will see how to add those using roads and rivers very soon.
• Thematic maps visualize specific Geometry types:
• Choropleth maps visualize Polygons.
• Dot maps visualize Points.
Figure 4. Geometry types for Choropleth and Dot maps.
Which of the following options of Thematic map types:
"choropleth_map"
"dot_distribution_map"
…corresponds to each of these Epidemic map figures seen below?
Code
# Write your answers here as comments:# Malaria cases in Africa : a# COVID-19 cases in the world: b
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
Which of the following options of Thematic map types corresponds to each of these Epidemic map figures? Your answer should be either “choropleth_map” or “dot_distribution_map”.
[**Malaria cases in Africa]** is a CHROPLETH MAP
[COVID-19 cases in the world] is a DOT DISTRIBUTION MAP
Source Code
---title: 'Thematic Maps'author: - name: 'Andree Valle Campos' - name: 'Laure Vancauwenberghe' - name: 'Kene David Nwosu'date: '2024-12-1'format: html: code-fold: true code-tools: true number-sections: true toc: true css: global/style/style.css---```{r setup, include = FALSE}# Load packages if(!require(pacman)) install.packages('pacman')pacman::p_load(tidyverse, knitr, here)# Source functions source(here('global/functions/misc_functions.R'))# knitr settingsknitr::opts_chunk$set(warning = FALSE, message = FALSE, class.source = 'tgc-code-block')``````{r theme-setup, echo=FALSE}ggplot2::theme_set(new = theme_bw())options(scipen=10000)```## PrerequisitesThis lesson requires the following packages:```{r, eval=FALSE, echo=TRUE, message=FALSE}if(!require('pacman')) install.packages('pacman')pacman::p_load_gh('afrimapr/afrilearndata')pacman::p_load(rnaturalearth, ggspatial, ggplot2, mdsr, tidyverse, dplyr, sf)```------------------------------------------------------------------------## Learning objectives1. Identify two types of **Thematic maps** (choropleth and dot maps) used by epidemiologist to visualize Geospatial data.2. Create Thematic maps using `{ggplot2}` and the **`geom_sf()`** function.3. Relate each Thematic map with a **Geometry** type.------------------------------------------------------------------------## Choropleth map### What is it? {.unnumbered}• *Colors or shadings* represent **geographic regions** in relation to the value of a variable.• E.g. *Larger values* could be indicated by a [**darker color**]{.underline}, and *smaller values* by a [**lighter color**]{.underline}.### How to plot it? {.unnumbered}• Geospatial data can be plotted using **`ggplot2::geom_sf()`**.• *Colors* and *shapes* can be depicted with the `aes()` function,• Using the `fill`, `color` and `size` arguments.#### With Quantitative data {.unlisted .unnumbered}• A Quantitative *Choropleth map* requires the `fill` argument.• Let's create one with the `africountries` dataset from {`afrilearndata}`!• Administrative boundaries of all the countries in the African continent.1. Use `ggplot2::geom_sf()` to plot African countries, only.2. `fill` each of them according to their population (`pop_est`):```{r}# WRITE YOUR CODE HERElibrary(sf)library(afrilearndata)africountries %>%mutate(geometry =st_as_text(geometry)) %>%as_tibble()ggplot(data = africountries) +geom_sf(mapping =aes(fill = pop_est))```::: side-note• `sf` stands for **"simple features"**, an [open standard](http://portal.opengeospatial.org/files/?artifact_id=25355) used to represent a wide range of *geometric shapes*.:::::: practiceCreate a Choropleth map with the `world` data from the `{spData}` package, using `geom_sf()`, to portrait its countries and `fill` them in relation to its population, available in the `pop` variable.```{r,eval = T}library(spData)# Write and visualize your answer:q1 <- world %>% ggplot() + geom_sf(mapping = aes(fill = pop))```:::#### With Categorical data {.unlisted .unnumbered}• Let's keep using `fill`.• Create a map with countries *colored* by their Economical classification (`economy`):```{r}countries <- rnaturalearth::ne_countries(returnclass ="sf")ggplot(data = countries) +geom_sf(mapping =aes(fill = economy))```::: watch-out• Before using `geom_sf()`, verify that your *Spatial data* is an `"sf"` R object:```{r}library(sf)library(afrilearndata)class(africountries)```• More about `sf` in the following lessons!:::### How to use it? {.unnumbered}• Visualize how one variable changes across a defined regions.{width="441"}• Region of interest (Sierra Leone)• Partitioned into a finite number of subregions (districts)• Number of cases aggregated at that level.::: vocab• *Choropleth maps* visualize a shape called **Polygons**.• Collects data from an enclosed region.• Partitioned into a finite number of *areal units* with defined boundaries.• E.g., data collected by ZIP code, census tract, or the administrative boundary levels of a country (Figure 2).:::------------------------------------------------------------------------## Dot map### What is it? {.unnumbered}• Thematic map type that uses **dots** to represent attribute values.### How to plot it? {.unnumbered}• The *Dot map* could use the `size` or `color` argument.#### With Quantitative data {.unlisted .unnumbered}• A Quantitative Dot map requires the `size` argument.• Let's create a Dot map!• Use the `africapitals` dataset, it contains the location of capital cities in the African continent.1. Use `ggplot2::geom_sf()` to plot these locations,2. and `size` them according to their number of inhabitants:```{r,eval=TRUE,echo=TRUE}library(sf)library(afrilearndata)africapitals %>% as_tibble()ggplot(data = africapitals) + geom_sf(mapping = aes(size = pop))```::: rstudio-cloudWe can replicate John Snow's Dot map with the number of deaths per household from the 1854 London cholera outbreak:```{r,eval=FALSE}cholera_deaths <- read_rds(here("data/cholera_deaths.rds"))ggplot(data = cholera_deaths) + geom_sf(mapping = aes(size = Count), alpha = 0.7)```:::#### With Categorical data {.unlisted .unnumbered}• Visualize airports *classified* by `type` using the `color` argument:```{r,results='hide'}airports <- rnaturalearth::ne_download(scale = 10, type = "airports", returnclass = "sf")ggplot(data = airports) + geom_sf(mapping = aes(color = type))```::: practiceCreate a Thematic map with the `afriairports` object to portrait all its airport locations, using `geom_sf()`, and `color` them in relation to the `type` variable.```{r,eval = FALSE}# Write and visualize your answer:q2 <- ggplot(data = afriairports) + geom_sf(mapping = aes(color = type))```:::### How to use it? {.unnumbered}• To visualize the *scatter of your data* and visually *scan for clusters*.{width="437"}::: vocab• *Dot maps* visualize a shape called **Point**.• Collects data that register the *locations* of random events.• E.g., geographical coordinates of individuals with a given diagnosis (Figure 3).:::::: side-note• Bothered by having just dots and no geographical context?• That's good! We will see how to add those using roads and rivers very soon.:::::: recap• Thematic maps visualize specific **Geometry** types:• Choropleth maps visualize *Polygons*.• Dot maps visualize *Points*.{width="445"}:::::: practiceWhich of the following options of *Thematic map* types:a. `"choropleth_map"`b. `"dot_distribution_map"`...corresponds to each of these *Epidemic map* figures seen below?```{r}# Write your answers here as comments:# Malaria cases in Africa : a# COVID-19 cases in the world: b```1. [Malaria cases in Africa](https://data.unicef.org/topic/child-health/malaria/):{width="446"}2. [COVID-19 cases in the world](https://coronavirus.jhu.edu/map.html):{width="613"}:::------------------------------------------------------------------------## Wrap up• We learned about *Thematic maps*,• How to create them using *ggplot2::geom_sf()*,• Which type of *Geometry* they visualize.• But, how can we complement Thematic maps with *geographic context*?• We are going to learn about how to add *Physical features* to our maps.------------------------------------------------------------------------## Contributors {.unlisted .unnumbered}The following team members contributed to this lesson:`r tgc_contributors_list(ids = c("avallecam", "lolovanco", "kendavidn"))`------------------------------------------------------------------------## References {.unlisted .unnumbered}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>`r tgc_license()`------------------------------------------------------------------------## Answer Key {.unnumbered}### Q1 {.unlisted .unnumbered}```{r}q1 <-ggplot(data = world) +geom_sf(aes(fill = pop))q1```### Q2 {.unlisted .unnumbered}```{r}q2 <-ggplot(data = afriairports) +geom_sf(aes(fill = type))q2```### Q3 & Q4 {.unlisted .unnumbered}Which of the following options of *Thematic map* types corresponds to each of these *Epidemic map* figures? Your answer should be either "choropleth_map" or "dot_distribution_map".1. [\*\*Malaria cases in Africa]\*\* is a CHROPLETH MAP2. **[COVID-19 cases in the world]** is a DOT DISTRIBUTION MAP