In this practical, you will create a map of Basel showing the distribution of wealth.

0 - Preliminaries

  1. If you haven’t done so already, load the tidyverse and the taxation.csv data.
library(tidyverse)
basel <- read_csv('1_data/taxation.csv')
  1. Load the sf package.
library(sf)

1 - Loading shapefiles

  1. Use read_sf to read in all of the shape files contained in the quarters folder and save the result as basel_map. The function will automatically read all files contained in the folder.
basel_map <- read_sf("1_data/quarters")
  1. Print basel_map in the console and try to make sense of its contents.

2 - Plotting a map

  1. Now, put Basel’s quarters on the map. Pipe the basel_map object into an otherwise empty ggplot() function and add geom_sf().
basel_map %>% 
  ggplot() + 
  geom_sf()
  1. Since we are not really interested in coordinates, remove the background by adding theme_void().
basel_map %>% 
  ggplot() + 
  geom_sf() + 
  XX
  1. If you like, you could already start styling the map, e.g., by coloring the borders and areas. To do this, set, for instance, col = "white" and fill = "blue". But the question is where? Try first making these settings inside ggplot(aes()).
basel_map %>% 
  ggplot(aes(XX = XX, XX = XX)) + 
  geom_sf() + 
  XX
  1. That didn’t work. The problem is that anything inside of aes() is interpreted as a variable, not as constant. Now make these settings inside of geom_sf() without using the aes() helper function. That’s how we usually set aesthetics to constants: inside of the geom, outside of aes().
basel_map %>% 
  ggplot() + 
  geom_sf(XX = XX, XX = XX) + 
  XX

3 - Adding variables

  1. You know already how to create a map. Now, we shall see how we can add variables, such as income and wealth and represent them in our map. To add the variables in our basel object, simply join it to the basel_map object by matching "TYPE" and "quarter". This is possible because basel_map is also a tibble. Save the result back to basel_map.
basel_map <- basel_map %>% 
  left_join(basel,
            by = c("TYPE" = "quarter"))
  1. Now that our taxation data has been added to basel_map, you can start representing the data in the map. How about instead of fill = "blue" you fill the areas according to wealth_mean. Since wealth_mean is a variable, place it into aes(). You can do this either inside ggplot() or geom_sf(). Note that if you forget to delete fill = "blue" nothing will happen, as setting fill to a constant will overwrite the settings made in aes().
basel_map %>% 
  ggplot(aes(XX = XX)) + 
  geom_sf(XX = XX) + 
  XX

4 - Styling

  1. Add appropriate labels using labs().

  2. Fix the legend title by adding, e.g., scale_fill_continuous(name = 'Wealth').

5 - Project work

  1. Try to use what you have learned in this section to either create a map based on your data. Do this even if your project visualization does not contain a map. If you are lacking the shapefiles visit https://www.diva-gis.org/datadown.