We’ve finally arrived at creating our first maps based on the data we imported and cleaned in the previous sessions. The focus of this first session lies on the package tmap to create maps.
Let’s start super simple. Import our enhanced district data. Create a map with only the outline of Germany and one where the districts’ borders are visible. Chose a color of your choice for both maps.
# load libraries
library(tmap)
## Warning: package 'tmap' was built under R version 4.0.3
library(sf)
## Warning: package 'sf' was built under R version 4.0.3
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# code based on the original
german_districts <- st_read(dsn = "../data",
layer = "GER_DISTRICTS") %>%
rename(., district_id = id)
## Reading layer `GER_DISTRICTS' from data source `C:\Users\annes\Documents\gesis-workshop-geospatial-techniques-R\data' using driver `ESRI Shapefile'
## Simple feature collection with 401 features and 1 field
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 5.86625 ymin: 47.27012 xmax: 15.04182 ymax: 55.05838
## geographic CRS: ETRS89
attributes_districts <- read.csv("../data/attributes_districts.csv",
header = T, fill = T, sep = ",")
german_districts_enhanced <-
german_districts %>%
left_join(., attributes_districts, by = "district_id") %>%
st_transform(., crs = 3035)
# start by setting the tmap mode to "plot" for static maps
tmap_mode("plot") # interactive maps can be designed by changing the mode to "view"
## tmap mode set to plotting
# first maps based on geometrc features
tm_shape(german_districts_enhanced) + # call the shapefile first
tm_fill(col = "lightgrey") # fills the polygons without drawing borders
# ... or use tm_polygons
tm_shape(german_districts_enhanced) +
tm_polygons(col = "lightblue") # fills the polygons and draws borders
In a second step, we want to visualize some information on the German districts contained in the attribute table. Choose the column cases_per_100k and create a map of Covid-19 cases per 100,000 inhabitants in Germany. Alternate the map by:
Assign your map to an object called covid_map.
# plot the covid-19 cases per 100.000 inhabitants
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k") # "col" can be the name of a color or a column name
# change the title of the legend and the color palette
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k", # add a title to the legend
palette = "RdPu") # change the color palette
# add a title in the color blue
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k",
palette = "RdPu") +
tm_layout(title = "Covid Cases in Germany", # alternate the overall layout like title
title.color = "blue" ) # changes the font color of the title
# place the legend outside of the map to the left
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k",
palette = "RdPu") +
tm_layout(title = "Covid Cases in Germany",
title.color = "blue" ) +
tm_legend(legend.outside = TRUE, # positions the legend outside
legend.outside.position = "left") # defines legend positions
# save your map in an object called "covid_map"
covid_map <-
tm_shape(german_districts_enhanced) +
tm_fill(col = "cases_per_100k",
title = "Covid-19 Cases per 100k",
palette = "RdPu") +
tm_layout(main.title = "Covid Cases in Germany",
main.title.color = "blue" ) +
tm_legend(legend.outside = TRUE,
legend.outside.position = "left")
Check the head() of your shapefile. You might need to rename some of the columns or use the correct names: cases_per_100k (abbreviated: cs__100).
Combine following options with a plus sign:
tm_fill(col = "", title = "", palette = "") +tm_layout(title = "", title.color = "") +tm_legend(legend.outside = T/N, legend.outside.position = "")If you run colors(), R returns the names of all built-in colors.
To assign your map to an object, use the arrow <-.