- Here we’ll reproduce Figure 7.2 (Bauer and Schulte-Cloos 2018)
- What does it show? What does the underlying data probably look like? What kind of variables are we dealing with?
- What do you like, what do you dislike about the figure? What is good, what is bad?
- What kind of information could we add to this figure?
- How would you approach the figure if you want to replicate it?
- How many scales/mappings does it use? Could we reduce them?
7.5.1 Lab: Data & Code
- Getting a better graphs of map data
- Learn how to plot shape files (polygons) with ggmaps
- Learn how to colour particular polyhons
- Learning how to plot several maps together
We start by importing the data, namely a shape file of Germany (you can get that here) as well as some voting data on the level of municipalities. You need to download the shape files from this link: https://drive.google.com/drive/folders/1LGm-kBDZhFc01ncBBvtFHPfC2eXXdooT?usp=sharing and change the path to the folder where you store them below.
# Load vote share data on the municipality level # data_votes_municipalities.csv data <- read_csv(sprintf("https://docs.google.com/uc?id=%s&export=download", "1dXeofymZeoh01V_INnkAPdfPru4xpLdw"), col_types = cols()) # Load shape files # Download the shape files: https://drive.google.com/drive/folders/1LGm-kBDZhFc01ncBBvtFHPfC2eXXdooT?usp=sharing # Adapt the folder "www/data" to your file location map_data <- readOGR(dsn="www/data", layer="VG250_GEM", encoding = "ASCII", verbose = FALSE) map_data <- sf::st_as_sf(map_data) # Convert to sf object # See column geometry map_data$AGS <- as.character(map_data$AGS)
Since, the map data is now stored as a
sf dataframe (
?class(map_data)) we can simply join it with other data.
The identifer we use to match the map data with out vote share data is called
AGS (Amtlicher Gemeindeschlüssel), a standard identifer for municipalities in Germany.
Let’s have a quick look at the map. Figure 7.3 plost the shape file with gray borders around the areas. It’s a bit convoluted since there are a lot of polygons definted in the map data (11435 municipalities):
In Figure 7.4 we add geographic area metadata, namely the vote share of the green party in 2017
share.greens2017 (this is simple as we add it to the dataframe beforehand):
- It zooms into German to show Bavaria on the lower right.
- It zooms into Bavaria to show the electoral district in the middle.
- It colours municipalities within the electoral district 233.
- It adds titles.
We’ll start by showing the different maps separatedly in a grid. Then we put them together.
# MAP 1: Bavaria within Germany map_data_states <- aggregate(map_data, by = list(map_data$SN_L), mean) # SN_L = STATE p1 <- ggplot() + geom_sf(data = map_data_states, fill = "white", color = "black", size = 0.1) + geom_sf(data = map_data_states %>% filter(Group.1 =="09"), fill = "black", color = "black") + theme_void() + ggtitle("Bavaria:\nLocation within Germany") + theme(plot.title = element_text(color="black", size=10, hjust = 0.5)) # MAP 2: Elector district within Bavaria map_data_bavaria <- map_data %>% filter(SN_L=="09") %>% dplyr::select("Wahlkreis") map_data_bav_elec_dist <- aggregate(map_data_bavaria, by = list(map_data_bavaria$Wahlkreis), mean) %>% select(Wahlkreis) map_data_bav_elec_dist_233 <- map_data_bav_elec_dist %>% filter(Wahlkreis == 233) p2 <- ggplot() + geom_sf(data = map_data_bav_elec_dist, fill = "white", color = "black", size = 0.1) + geom_sf(data = map_data_bav_elec_dist_233, fill = "black", color = "black") + #geom_sf(data = map_electoral_district_233_bb, fill = NA, color = "red", size = 0.8) + theme_void() + ggtitle("Electoral district 233:\nLocation within Bavaria") + theme(plot.title = element_text(color="black", size=10, hjust = 0.5)) # MAP 3: map_data_mun_dist_233 <- map_data %>% filter(Wahlkreis==233) map_color_black <- map_data_mun_dist_233 %>% filter(Wahlkreis==233, municipality=="Regensburg") map_color_black2 <- map_data_mun_dist_233 %>% filter(Wahlkreis==233, municipality=="Regenstauf, M") map_color_gray <- map_data_mun_dist_233 %>% filter(Wahlkreis==233, municipality!="Regensburg") p3 <- ggplot() + geom_sf(data = map_data_mun_dist_233, fill = NA, colour = "black", size = 0.1) + geom_sf(data = map_color_gray, fill = "lightgray", colour = "black", size = 0.1) + geom_sf(data = map_color_black, fill = "black", colour = "black", size = 0.1) + geom_sf(data = map_color_black2, fill = "black", colour = "black", size = 0.1) + theme_void() + ggtitle("Electoral district 233: Municipalities with (black) and\nwithout (gray) local candidates") + theme(legend.position = "none", axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), panel.background = element_blank(), plot.margin = unit(c(0,0,0,0), "cm"), plot.title = element_text(color="black", size=10, hjust = 0.5)) grid.arrange(p1, p2, p3, ncol=3)
Subsequently, we plot all three maps together in Figure 7.6:
grid.arrange() in Figure 7.7:
Bauer, Paul C, and Julia Schulte-Cloos. 2018. “Local Heroes? The Effect of Candidates’ Place of Residence on Electoral Success.” In EPSA Conference, Vienna.