## 7.5 Graph

• Here we’ll reproduce Figure 7.2 (Bauer and Schulte-Cloos 2018)
• Questions:
• 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

• The code for Figure 7.2 is shown below (and creates Figure 7.6).

• Learning objectives

• 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
"1dXeofymZeoh01V_INnkAPdfPru4xpLdw"),
col_types = cols())
# Load shape files
# 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.

map_data <- left_join(map_data, data, by="AGS")

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):

ggplot() +
geom_sf(data = map_data,
fill = "white",
color = "black",
size = 0.001)

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):

ggplot() +
geom_sf(data = map_data,
aes(fill = share.greens2017), colour = NA) + # fill but turn of borders
scale_fill_gradient(low = "white", high = "darkgreen", na.value = NA)

Now let’s try visualizing 7.2. In contrast, to Figure 7.3 and 7.4, it doesn’t show all of Germany. Rather it is used to illustrate a comparative strategy.

1. It zooms into German to show Bavaria on the lower right.
2. It zooms into Bavaria to show the electoral district in the middle.
3. It colours municipalities within the electoral district 233.
4. 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:

gg_inset_map <- ggdraw() +
draw_plot(p3) +
draw_plot(p1, x = 0.015, y = 0.05, width = 0.25, height = 0.25) +
draw_plot(p2, x = 0.25, y = 0.05, width = 0.25, height = 0.25)

gg_inset_map

Or use grid.arrange() in Figure 7.7:

grid.arrange(p1,p2,p3,
ncol = 2, nrow = 2,
layout_matrix = rbind(c(1,2), c(3,3)))

### References

Bauer, Paul C, and Julia Schulte-Cloos. 2018. “Local Heroes? The Effect of Candidates’ Place of Residence on Electoral Success.” In EPSA Conference, Vienna.