+ - 0:00:00
Notes for current slide
Notes for next slide

POL 304: Using Data to Understand Politics and Society

Drawing Maps

Olga Chyzh [www.olgachyzh.com]

1 / 21

Dependence in Observational Data

  • Individuals are nested in social networks

    • Individual decisions are influenced by their friends.
  • Provinces are surrounded by other provinces

    • Provinces mimic one another's policies
  • Country-level outcomes are often a result of negotiations with other countries:

    • Economic or environmental policies
2 / 21

Three Mechanisms for Spatial Dependence

  • Homophily---similarity in outcomes is endogenous, units are similar because they self-select into the same outcome (e.g., partisan geo-sorting)

  • Common exposure---similarity in outcomes is driven by an exogenous factor that affects nearby units (the effect of earthquakes on housing prices)

  • Diffusion---nearby units affect each other through learning, imitation, etc (e.g., policy diffusion)

3 / 21

Source: van Weezel S. "On climate and conflict: Precipitation decline and communal conflict in Ethiopia and Kenya." Journal of Peace Research. 2019;56(4):514--528.

4 / 21

Source: Chyzh, Olga V. and R. Urbatsch. 2021. "Bean Counters: The Effect of Soy Tariffs on Change in Republican Vote Share Between the 2016 and 2018 Elections."Journal of Politics 83 (1): 415--419.

5 / 21

What You Need

  • Latitude/longitude points for all map boundaries

  • Need to know to which boundary/state lat/long points belong

  • Need to know the order to connect points within each group

6 / 21

Making Maps

library(tidyverse)
library(mapproj)
library(maps)
library(mapdata)
states <- map_data("state")
head(states)
## long lat group order region subregion
## 1 -87.46201 30.38968 1 1 alabama <NA>
## 2 -87.48493 30.37249 1 2 alabama <NA>
## 3 -87.52503 30.37249 1 3 alabama <NA>
## 4 -87.53076 30.33239 1 4 alabama <NA>
## 5 -87.57087 30.32665 1 5 alabama <NA>
## 6 -87.58806 30.32665 1 6 alabama <NA>
7 / 21

A Basin (Rather Hideous) Map

library(ggplot2)
ggplot() + geom_path(data=states, aes(x=long, y=lat, group=group),color="black", size=.5)

8 / 21

A Bit Nicer of a Map

#Set theme options:
theme_set(theme_grey() + theme(axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="none"))
ggplot() + geom_path(data=states, aes(x=long, y=lat, group=group),color="black", size=.5)+ coord_map()
9 / 21

Polygon instead of Path

ggplot() + geom_polygon(data=states, aes(x=long, y=lat, group=group),color="black", size=.5)+ coord_map()

10 / 21

Incorporate Information About States

  • Add other geographic information (e.g., counties) by adding geometric layers to the plot

  • Add non-geographic information by altering the fill color for each state

    • Use geom = "polygon" to treat states as solid shapes to add color

    • Incorporate numeric information using color shade or intensity

    • Incorporate categorical informaion using color hue

11 / 21

Categorical Information Using Hue

If a categorical variable is assigned as the fill color then ggplot will assign different hues for each category.

Let’s load in a state regions dataset:

statereg<- read.csv("./data/statereg.csv")
head(statereg)
## State StateGroups
## 1 california West
## 2 nevada West
## 3 oregon West
## 4 washington West
## 5 idaho West
## 6 montana West
12 / 21

Join the Data

states.class.map <- left_join(states, statereg, by = c("region" = "State"))
head(states.class.map)
## long lat group order region subregion StateGroups
## 1 -87.46201 30.38968 1 1 alabama <NA> South
## 2 -87.48493 30.37249 1 2 alabama <NA> South
## 3 -87.52503 30.37249 1 3 alabama <NA> South
## 4 -87.53076 30.33239 1 4 alabama <NA> South
## 5 -87.57087 30.32665 1 5 alabama <NA> South
## 6 -87.58806 30.32665 1 6 alabama <NA> South
13 / 21

Plot the Regions

ggplot() + geom_polygon(data=states.class.map, aes(x=long, y=lat, group=group, fill = StateGroups), colour = I("black"))+ coord_map()+theme(legend.position="bottom")

14 / 21

Map of Canada

library(devtools)
install_github("mccormackandrew/mapcan", build_vignettes = TRUE)
library(mapcan)
canada_map<-mapcan(boundaries = "province", type="standard",territories=TRUE)
head(canada_map)
## long lat order hole piece pr_sgc_code group pr_english
## 1 2278204 1261783 1 FALSE 1 10 10.1 Newfoundland and Labrador
## 2 2294272 1251745 2 FALSE 1 10 10.1 Newfoundland and Labrador
## 3 2300435 1250534 3 FALSE 1 10 10.1 Newfoundland and Labrador
## 4 2306656 1243299 4 FALSE 1 10 10.1 Newfoundland and Labrador
## 5 2315548 1241630 5 FALSE 1 10 10.1 Newfoundland and Labrador
## 6 2313319 1237013 6 FALSE 1 10 10.1 Newfoundland and Labrador
## pr_french pr_alpha
## 1 Terre-Neuve-et-Labrador NL
## 2 Terre-Neuve-et-Labrador NL
## 3 Terre-Neuve-et-Labrador NL
## 4 Terre-Neuve-et-Labrador NL
## 5 Terre-Neuve-et-Labrador NL
## 6 Terre-Neuve-et-Labrador NL
15 / 21

Map of Canada

#Set theme options:
theme_set(theme_grey() + theme(axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.position="none"))
ggplot(canada_map, aes(long, lat, group = group)) +
geom_polygon(color="black", fill="white")
16 / 21

Map of Canada

17 / 21

Canada Election Results

library(tidyverse)
library(magrittr)
data("federal_election_results")
federal_election_results %>% as.data.frame() %>%
dplyr::filter(election_year=="2015")->electdata
canada_ridings<-mapcan(boundaries = "ridings", type="standard",territories=TRUE)
head(canada_ridings)
canada_ridings %>% left_join(electdata, by="riding_code") %>%
ggplot(aes(long, lat, group = group, fill=factor(party)))+
geom_polygon(color="black") +scale_fill_discrete("Party", type="qual") + theme(legend.position="bottom")
18 / 21

Canada Election Results

19 / 21

Your Turn

  1. Check out the help file for the map_data function to find out how to make a map of the world.

  2. Can you figure out how to remove Antarctica from the map?

  3. Use the GDP data you scraped as part of our web-scraping class to shade countries based on their GDP.

20 / 21

Your Turn (Advanced)

  1. Read in the animal.csv data:
  1. Plot the location of animal sightings on a map of the region
  2. On this plot, try to color points by class of animal and/or status of animal
  3. Use color to indicate time.

21 / 21

Dependence in Observational Data

  • Individuals are nested in social networks

    • Individual decisions are influenced by their friends.
  • Provinces are surrounded by other provinces

    • Provinces mimic one another's policies
  • Country-level outcomes are often a result of negotiations with other countries:

    • Economic or environmental policies
2 / 21
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow