class: center, middle, inverse, title-slide .title[ # POL 304: Using Data to Understand Politics and Society ] .subtitle[ ## Drawing Maps ] .author[ ### Olga Chyzh [www.olgachyzh.com] ] --- ## 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 --- ## 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) --- <img src="./images/communal_violence.png" width="800px" style="display: block; margin: auto;" /> 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. --- <img src="./images/elections.png" width="600px" style="display: block; margin: auto;" /> 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. --- ## 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 --- ## Making Maps ```r 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> ``` --- ## A Basin (Rather Hideous) Map ```r library(ggplot2) ggplot() + geom_path(data=states, aes(x=long, y=lat, group=group),color="black", size=.5) ``` <img src="11_maps_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- ## A Bit Nicer of a Map ```r #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() ``` --- ## Polygon instead of Path ```r ggplot() + geom_polygon(data=states, aes(x=long, y=lat, group=group),color="black", size=.5)+ coord_map() ``` <img src="11_maps_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- ## 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 --- ## 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: ```r 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 ``` --- ## Join the Data ```r 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 ``` --- ## Plot the Regions ```r 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") ``` <img src="11_maps_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- ## Map of Canada ```r 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 ``` --- ## Map of Canada ```r #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") ``` --- ## Map of Canada <img src="11_maps_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- ## Canada Election Results ```r 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") ``` --- ## Canada Election Results <img src="11_maps_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> --- ## 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. <img src="11_maps_files/figure-html/unnamed-chunk-15-1.png" width="400px" style="display: block; margin: auto;" /> --- ## Your Turn (Advanced) 1. Read in the animal.csv data: 2. Plot the location of animal sightings on a map of the region 3. On this plot, try to color points by class of animal and/or status of animal 4. Use color to indicate time. <img src="11_maps_files/figure-html/unnamed-chunk-17-1.png" style="display: block; margin: auto;" />