Let us go back to where we stopped developing our mapping techniques yesterday: Our Covid-19 Map. This is just a super short repetition of what was just presented. No worries, we’re going to have some time to improve your maps in the second exercise.
Recreate a simple Covid-19 map as already created yesterday but use ggplot2 this time.
They don’t have to match perfectly but:
# For importing the data, you can use this code:
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
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
attributes_districts <- read.csv("../data/attributes_districts.csv",
header = T, fill = T, sep = ",")
german_districts_enhanced <- st_read(dsn = "../data",
layer = "GER_DISTRICTS") %>%
rename(., district_id = id) %>%
st_transform(., crs = 3035) %>%
left_join(., attributes_districts, by = "district_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
# load libraries
library(ggplot2)
# build map
covid_map <-
ggplot() +
geom_sf(data = german_districts_enhanced,
aes(fill = cases_per_100k),
color = NA) +
scale_fill_viridis_c(option = "plasma",
direction = -1,
name = "Covid-19 Cases per 100k") +
theme(legend.position = "left") +
labs(title="Covid-19 Map",
subtitle= "German Federal Elections 2017")
covid_map
Save your map as a .pdf!
You need the function ggsave to fulfill this task.
# save map
ggsave("./own_material/covid_map.pdf", covid_map, dpi = 300)
Now we want to add another layer to see if enough hospitals are located in the Covid-19 high-risk zones of Germany.
For an extra challenge which we haven’t covered: The hospital shapefile contains information on the number of beds in each hospital. Can you change the size of hospital dots according to the number of beds?
Make sure that the CRS of your hospital layer is defined correctly!
You need to define size = beds as the aesthetics of the new layer.
The variable beds is a character but need to be numeric.
# import point layer hospital sf
hospitals_sf <- read.csv("../data/hospital_points.csv", header = T, fill = T, sep = ",") %>%
st_as_sf(., coords = c("X", "Y"),
crs = 3035)
# add hospital layer to map
covid_hospitals_map <-
ggplot() +
geom_sf(data = german_districts_enhanced,
aes(fill = cases_per_100k),
color = NA) +
scale_fill_viridis_c(option = "plasma",
direction = -1,
name = "Covid-19 Cases per 100k") +
theme(legend.position = "left") +
labs(title="Covid-19 Map") +
# add hospital layer
geom_sf(data = hospitals_sf,
# define the size of the hospital layer
# by the number of beds
aes(size = as.numeric(beds)))
covid_hospitals_map
## Warning in FUN(X[[i]], ...): NAs introduced by coercion
## Warning in FUN(X[[i]], ...): NAs introduced by coercion
## Warning: Removed 53 rows containing missing values (geom_sf).