Sometimes Dot maps can have overlapped points that difficult the visualization of more specific patterns. Additionally, most times spatial data requires more explicit Google Maps-like geographic context.
In this lesson, we are going to learn about how use Density maps to avoid overlaps in them, and how to add Basemaps for Google Maps-like backgrounds.
Identify one more type of Thematic map (Density maps) used by epidemiologist to visualize overlapping Geospatial data.
Complement Thematic maps with Basemaps
for Google Maps-like backgrounds using the
annotation_map_tile()
function, from the
{ggspatial}
package.
This lesson requires the following packages:
if(!require('pacman')) install.packages('pacman')
pacman::p_load_gh("afrimapr/afrilearndata")
pacman::p_load_gh("avallecam/epihelper")
pacman::p_load(ggspatial,
ggplot2,
tibble,
terra,
dplyr,
spData,
sf,
prettymapr)
This lesson requires familiarity with {ggplot2}
: if you
need to brush up, have a look at our introductory course on data
visualization.
A Density map is a type of Thematic map where colours are used to represent intensity of a value, however, it does not use defined regions or geopolitical boundaries to group data. This type of map is typically used for showing ‘hot spots’ or areas with a high density or concentration of points.
As an example, we are going to use the afriairports
dataset, from the {afrilearndata}
package, that contains
the locations of African airports.
With a Dot map we get overlapping points using the
geom_sf()
function, as in here:
A Density map with {ggplot2}
will require
four main steps. Let’s use the
afriairports
dataset as an example:
epihelper::st_coordinates_tidy()
to
retrieve the point coordinates.ggplot()
function to define the
new coordinates column names X
and
Y
,geom_bin_2d()
function to depict the number
of airports per area,coord_sf()
function to
transform the figure X and Y
axis:
afriairports %>%
# 👉 (1) extract coordinates
st_coordinates_tidy() %>%
# 👉 (2) define new coordinates with ggplot()
ggplot(aes(x = X, y = Y)) +
# 👉 (3) with a new geom function
geom_bin_2d() +
# 👉 (4) transform axis
coord_sf()
Create a Density map with the pcrime
data read from the
pcrime.rds
local file.
Use the geom_bin_2d()
, to portrait the number of crimes
per area, faceted by the two types of crime in the column
marks
.
This type of Thematic map is best used with Environmental data, such as altitude, air pollution or rainfall data values measured at several monitoring stations.
Density maps are also used to visualize spatially continuous disease prevalence surfaces. For example, Moraga et al. (2019) used the prevalence values of malaria in children obtained from surveys conducted at 65 villages in The Gambia to predict the disease prevalence at unobserved locations, using a geostatistical model.
There are two {ggplot2}
alternatives to plot Density
maps:
In one hand, the geom_density_2d_filled()
function
creates a contoured density plot to identify clusters of
"count"
values:
afriairports %>%
# (1) extract coordinates
st_coordinates_tidy() %>%
# (2) start ggplot
ggplot(aes(x = X, y = Y)) +
# 👉 with an alternative geom function 👈
geom_density_2d_filled(contour_var = "count") +
# (4) transform axis
coord_sf()
In the other hand, the stat_density_2d()
allows to
create a continuous surface of counted values from point data:
afriairports %>%
# (1) extract coordinates
st_coordinates_tidy() %>%
# (2) start ggplot
ggplot(aes(x = X, y = Y)) +
# 👉 with an alternative geom function 👈
stat_density_2d(
geom = "raster",
mapping = aes(fill = after_stat(count)),
contour = FALSE) +
scale_fill_viridis_c() +
# (4) transform axis
coord_sf()
Use the st_coordinates_tidy()
function to extract the X
and Y coordinates from the pcrime
dataset.
This will be useful to built a Density map with
geom_density_2d_filled()
and portrait the number of crimes
per area, faceted by the two types of crime in the column
marks
.
For all our previous maps, we only have partial context for what we are seeing. For a more integrative view, we may want to overlay our map over Google Maps-like physical features. These are called Basemaps.
For example, for our London cholera outbreak Dot map, we want to
overlay it on the London street map—and this is exactly what
{ggspatial}
lets us do.
The annotation_map_tile()
function adds a layer of
map tiles pulled from Open Street Map. We can
control the zoom
level. Here, we map the number of deaths
at each location to the size of the dot.
Add a Basemap to a Density map using the pcrime
object
and the annotation_map_tile()
function.
pcrime %>%
# (1) extract coordinates
st_coordinates_tidy() %>%
# (2) start ggplot
ggplot(aes(x = X, y = Y)) +
# 👉 add a basemap 👈
annotation_map_tile(zoomin = 0) +
# (3) with a new geom function
geom_bin_2d() +
# (4) transform axis
coord_sf() +
# facet
facet_wrap(~marks)
geom_*
function!In this lesson, we have learned about one more type of Thematic map called Density maps to avoid overlapping spatial points. Also, how to add Google Maps-like backgrounds called Basemaps.
But, How can we create more Thematic maps from external Spatial data created by other GIS software? In the following lessons, we are going to learn how to read external Spatial data and convert foreign objects to sf files! Follow along with the lessons to find how to train these skills!
## Error: <text>:2:9: unexpected '@'
## 1: pcrime %>%
## 2: @
## ^
## Error: <text>:2:9: unexpected '@'
## 1: pcrime %>%
## 2: @
## ^
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.