Practical 2

Spatial data visualisation: In search of the finest and ugliest map!

In this practical you are going to let your creativity unleash… for good 👼 and for evil 😈

You will be creating the BEST and WORST spatial data visualisations you can imagine.

Inspiration

For ideas on nice (or decent) maps see #30DayMapChallenge map gallery compiled by David Frigge. The maps have also the underlying code to reproduce them.

For the worst map see terrible maps and viz.wtf for inspiration.

You can use any data visualisation package in R (see some suggestions and guides below).

Package documentation is crucial! Read what the developers of the packages have arduously worked on for your ease of using their package.

The internet will be your main guide! Make sure to ask/prompt your questions correctly so that you get good code.

You are free to use ANY spatial data you have! If you can’t find anything suitable, you can use the datasets shared in class. See below for data packages you can use as base layers.

We aim for static maps that you will be saving as a PNG file and submitting to blackboard alongside the code you used to create the maps (either as an R script or a .qmd). Don’t forget to include any data you used that is not publicly accessible.

Due date: 05.05.2025

Grading criteria

  • Code should be fully reproducible
  • All data should be attached in the submission or available online
  • Use of extra packages (e.g. ggtext, ggsci, etc.) or exploration of customising functions beyond the typical with ggplot2 and tmap (e.g. changing size of legend items, custom palette, etc.)
  • Creativity
  • Documentation

You have plenty of time for this practical! In the next sessions you will be using plots to visualise your data, so hopefully you get the hang of it.

If you have trouble or questions, don’t hesitate to ask! If you had an idea, and did not find ANY way to do that with the available R packages out there, write it in your .R file (as a comment) or in your .qmd.

We will be doing a showcase of your maps in the session of the 09.05.2025

Data visualisation packages

{ggplot2}

Part of the tidyverse, ggplot2 is one of the go to packages for data visualisation in R.

There are plenty of tutorials out there on how to use ggplot2 for your data visualisation. Here is a very nice one by Cedric Scherer and another three-part one focused on maps by Mel Moreno and Mathieu Basille.

You will find native support to plot both sf (geom_sf()) and stars (raster; geom_stars()) objects.

Choosing ggplot will allow you to use some of its many extensions. For example, the package {ggtext} let’s you customise colors in the text in your plot. For spatial elements such as north arrows and scales you can check out {ggspatial}.

To save your ggplot you can use the following:

mymap = ggplot() +
  geom_sf(...) +
  theme(...)

ggsave(filename = "path/to/your/plot/location/mymap.png", plot = mymap,
       width = 15, height = 10, units = "cm", dpi = 300) # adjust as necessary

{tmap}

An R package for drawing thematic maps. This package tries to follow on the grammar of graphics principles of {ggplot2} but also provides a bit more flexibility. I personally use it for its versatility in switching between static and interactive, for its ease to build facet plots with free spatial axes and for the thought they put in particular cartography principles.

Version 4.0 has been cooking for a while and you will find the documentation here. You will also find a guide for making maps with {tmap} in the Geocomputation with R book.

To save your tmap you can use the following:

mymap = tm_shape() +
  tm_fill()

tmap_save(tm = mymap, filename = "path/to/your/plot/location/mymap.png",
       width = 15, height = 10, units = "cm", dpi = 300) # adjust as necessary

Base R

Although not the easiest to customise, you can definitely make nice maps using base R. Can you also make ugly maps with it?

You will find some basics on plotting sf objects in the {sf} plotting vignette in the package documentation here.

To save your plot to disk you can use the following code:

png(filename = "path/to/your/plot/location/mymap.png",
    width = 15, height = 10, units = "cm") # adjust as necessary

plot(...) # the code for your plot goes here

device.off()

With this code your plot will not render on the RStudio viewer but directly on the file you created. I recommend you to wrap your plot() code at the complete end, once you are done with your editing.

More resources

Data packages

{rnaturalearth}

Natural Earth is a public domain map dataset including vector country and other administrative boundaries. With the package you can access the data easily within R and load it as an sf object. See the package documentation here.

{osmdata}

To fetch data from OpenStreetMap you can use this package. Check the documentation on how to query data for your area here. If your data is very large you can also use the package {osmextract} to not exhaust the OSM API.

{elevatr}

To get elevation data you can use this package. See the package vignettes here and the GitHub repository here