class: center middle main-title section-title-1 # .kjh-yellow[Maps and]<br /> .kjh-lblue[Spatial Data] .class-info[ **Week 09 (Part 1)** .light[Kieran Healy<br> Duke University, Spring 2023] ] --- layout: true class: title title-1 --- # Load our libraries .SMALL[ ```r library(here) # manage file paths library(socviz) # data and some useful functions library(tidyverse) # your friend and mine library(tidycensus) # Tidily interact with the US Census library(maps) # Some basic maps ``` ``` ## ## Attaching package: 'maps' ``` ``` ## The following object is masked from 'package:purrr': ## ## map ``` ```r library(sf) # Make maps in ggplot library(tigris) # Talk to the Census's TIGER data ``` ``` ## To enable caching of data, set `options(tigris_use_cache = TRUE)` ## in your R script or .Rprofile. ``` ```r library(ggforce) # Useful enhancements to ggplot ``` ] --- class: center middle main-title section-title-1 # .huge[.kjh-lblue[Choropleths]] --- layout: false .center[] .right.w90.small[State-level; vote share; diverging; binned into four categories.] --- layout: false .center[] .right.w90.small[County level; winner only] --- layout: false .center[] .right.w90.small[County level vote share; diverging; binned into six categories] --- layout: false .center[] .right.w90.small[County level vote share; diverging continuous; purple midpoint] --- layout: false .center[] .right.w90.small[County level vote share; purple midpoint; county area deformed in proportion to population. By Mark Newman] --- layout: false .center[] .right.w90.small[Electoral college cartogram] --- layout: true class: title title-1 --- layout: false class: main-title main-title-inv middle # .middle.squish4.medium[.kjh-lblue[Problems showing] .kjh-orange[Non-Spatial Data]<br /> .kjh-lblue[in choropleth maps]] --- .center[] .right.w90.big[Pretty, Big, and Pretty Empty] --- .center[] .right.w90.big[Pretty, Big, and Pretty Empty] --- layout: true class: title title-1 --- # Aside: What the hell's that? .center[] --- # Zoom and Enhance .center[] .center.large[Suprisingly, not a coding error on my part.] --- # It's the .kjh-pink[Transcontinental Railroad] .center[] .center.medium[Making its way through the .kjh-lblue[Great Basin], America's largest .kjh-orange[endorheic watershed]. The checkerboard is a deliberate assignation of property rights along the borders of the railway line.] --- # Still with us, too .center[] .center.medium[Not identical, as Interstate 80 was able to go through some parts the railroad had to go around. OK, now back to scheduled programming.] --- layout: true class: title title-1 --- class: right bottom main-title section-title-1 ## .huge.right.bottom.squish4[.kjh-yellow[U.S. State-Level]<br />.kjh-lblue[Election Data]] --- # Set up the data .pull-left.w60[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") ``` .smaller[ | _Decimal_ | _Binary_ | _Hexadecimal_ | |---------|--------:|-------------:| | 0 | 0 | 0 | | 1 | 1 | 1 | | 2 | 10 | 2 | | 3 | 11 | 3 | | 4 | 100 | 4 | | 5 | 101 | 5 | | 6 | 110 | 6 | | 7 | 111 | 7 | | 8 | 1000 | 8 | | 9 | 1001 | 9 | | 10 | 1010 | A | | 11 | 1011 | B | | 12 | 1100 | C | | 13 | 1101 | D | | 14 | 1110 | E | | 15 | 1111 | F | ] ] -- .pull-right.w40[ ## `#`.kjh-red[`2E`].kjh-green[`116`].kjh-blue[`192`] .center[] ] ??? Hexadecimal numbers are "natural" to computers, because computers store and handle binary digits, and four binary digits make one hexadecimal digit. So one hexadecimal digit can have 16 different values (0 to 15 in decimal), and two hexadecimal digits together (called a "byte") can make 16×16=256 different levels of color. Because each of the three colors can have values from 0 to 255 (256 possible values), there are: 256 × 256 × 256 = 16,777,216 possible color combinations --- # Set up the data ```r election |> select(state, total_vote, r_points, pct_trump, party, census) ``` ``` ## # A tibble: 51 × 6 ## state total_vote r_points pct_trump party census ## <chr> <dbl> <dbl> <dbl> <chr> <chr> ## 1 Alabama 2123372 27.7 62.1 Republican South ## 2 Alaska 318608 14.7 51.3 Republican West ## 3 Arizona 2604657 3.5 48.1 Republican West ## 4 Arkansas 1130635 26.9 60.6 Republican South ## 5 California 14237893 -30.0 31.5 Democratic West ## 6 Colorado 2780247 -4.91 43.2 Democratic West ## 7 Connecticut 1644920 -13.6 40.9 Democratic Northeast ## 8 Delaware 443814 -11.4 41.7 Democratic South ## 9 District of Columbia 311268 -86.8 4.09 Democratic South ## 10 Florida 9502747 1.19 48.6 Republican South ## # … with 41 more rows ``` --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red *party_colors <- c("#2E74C0", "#CB454A") ``` ] .panel2-reveal-elecfacet1-auto[ ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") *election ``` ] .panel2-reveal-elecfacet1-auto[ ``` ## # A tibble: 51 × 22 ## state st fips total…¹ vote_…² winner party pct_m…³ r_poi…⁴ d_poi…⁵ ## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> ## 1 Alabama AL 1 2.12e6 588708 Trump Repu… 0.277 27.7 -27.7 ## 2 Alaska AK 2 3.19e5 46933 Trump Repu… 0.147 14.7 -14.7 ## 3 Arizona AZ 4 2.60e6 91234 Trump Repu… 0.035 3.5 -3.5 ## 4 Arkansas AR 5 1.13e6 304378 Trump Repu… 0.269 26.9 -26.9 ## 5 California CA 6 1.42e7 4269978 Clint… Demo… 0.300 -30.0 30.0 ## 6 Colorado CO 8 2.78e6 136386 Clint… Demo… 0.0491 -4.91 4.91 ## 7 Connecticut CT 9 1.64e6 224357 Clint… Demo… 0.136 -13.6 13.6 ## 8 Delaware DE 10 4.44e5 50476 Clint… Demo… 0.114 -11.4 11.4 ## 9 District of… DC 11 3.11e5 270107 Clint… Demo… 0.868 -86.8 86.8 ## 10 Florida FL 12 9.50e6 112911 Trump Repu… 0.0119 1.19 -1.19 ## # … with 41 more rows, 12 more variables: pct_clinton <dbl>, pct_trump <dbl>, ## # pct_johnson <dbl>, pct_other <dbl>, clinton_vote <dbl>, trump_vote <dbl>, ## # johnson_vote <dbl>, other_vote <dbl>, ev_dem <dbl>, ev_rep <dbl>, ## # ev_oth <dbl>, census <chr>, and abbreviated variable names ¹total_vote, ## # ²vote_margin, ³pct_margin, ⁴r_points, ⁵d_points ``` ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> * filter(st %nin% "DC") ``` ] .panel2-reveal-elecfacet1-auto[ ``` ## # A tibble: 50 × 22 ## state st fips total_…¹ vote_…² winner party pct_m…³ r_poi…⁴ d_poi…⁵ ## <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl> <dbl> ## 1 Alabama AL 1 2123372 588708 Trump Repu… 0.277 27.7 -27.7 ## 2 Alaska AK 2 318608 46933 Trump Repu… 0.147 14.7 -14.7 ## 3 Arizona AZ 4 2604657 91234 Trump Repu… 0.035 3.5 -3.5 ## 4 Arkansas AR 5 1130635 304378 Trump Repu… 0.269 26.9 -26.9 ## 5 California CA 6 14237893 4269978 Clint… Demo… 0.300 -30.0 30.0 ## 6 Colorado CO 8 2780247 136386 Clint… Demo… 0.0491 -4.91 4.91 ## 7 Connecticut CT 9 1644920 224357 Clint… Demo… 0.136 -13.6 13.6 ## 8 Delaware DE 10 443814 50476 Clint… Demo… 0.114 -11.4 11.4 ## 9 Florida FL 12 9502747 112911 Trump Repu… 0.0119 1.19 -1.19 ## 10 Georgia GA 13 4141447 211141 Trump Repu… 0.051 5.09 -5.09 ## # … with 40 more rows, 12 more variables: pct_clinton <dbl>, pct_trump <dbl>, ## # pct_johnson <dbl>, pct_other <dbl>, clinton_vote <dbl>, trump_vote <dbl>, ## # johnson_vote <dbl>, other_vote <dbl>, ev_dem <dbl>, ev_rep <dbl>, ## # ev_oth <dbl>, census <chr>, and abbreviated variable names ¹total_vote, ## # ²vote_margin, ³pct_margin, ⁴r_points, ⁵d_points ``` ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> * ggplot(mapping = aes(x = r_points, * y = reorder(state, r_points), * color = party)) ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_04_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + * geom_vline(xintercept = 0, * color = "gray30") ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_05_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + * geom_point(size = 2) ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_06_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + * scale_color_manual(values = party_colors) ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_07_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + scale_color_manual(values = party_colors) + * scale_x_continuous(breaks = c(-30, -20, -10, 0, * 10, 20, 30, 40), * labels = c("30\n (Clinton)", * "20", "10", "0", * "10", "20", "30", * "40\n(Trump)")) ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_08_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + scale_color_manual(values = party_colors) + scale_x_continuous(breaks = c(-30, -20, -10, 0, 10, 20, 30, 40), labels = c("30\n (Clinton)", "20", "10", "0", "10", "20", "30", "40\n(Trump)")) + * facet_wrap(~ census, ncol=2, * scales="free_y") ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_09_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + scale_color_manual(values = party_colors) + scale_x_continuous(breaks = c(-30, -20, -10, 0, 10, 20, 30, 40), labels = c("30\n (Clinton)", "20", "10", "0", "10", "20", "30", "40\n(Trump)")) + facet_wrap(~ census, ncol=2, scales="free_y") + * guides(color = "none") ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_10_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + scale_color_manual(values = party_colors) + scale_x_continuous(breaks = c(-30, -20, -10, 0, 10, 20, 30, 40), labels = c("30\n (Clinton)", "20", "10", "0", "10", "20", "30", "40\n(Trump)")) + facet_wrap(~ census, ncol=2, scales="free_y") + guides(color = "none") + * labs(x = "Point Margin", y = NULL) ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_11_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + scale_color_manual(values = party_colors) + scale_x_continuous(breaks = c(-30, -20, -10, 0, 10, 20, 30, 40), labels = c("30\n (Clinton)", "20", "10", "0", "10", "20", "30", "40\n(Trump)")) + facet_wrap(~ census, ncol=2, scales="free_y") + guides(color = "none") + labs(x = "Point Margin", y = NULL) + * theme(axis.text=element_text(size=8)) ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_12_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Look before Mapping .panel1-reveal-elecfacet1-auto[ ```r ## Hex color codes for Democratic Blue and Republican Red party_colors <- c("#2E74C0", "#CB454A") election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + scale_color_manual(values = party_colors) + scale_x_continuous(breaks = c(-30, -20, -10, 0, 10, 20, 30, 40), labels = c("30\n (Clinton)", "20", "10", "0", "10", "20", "30", "40\n(Trump)")) + facet_wrap(~ census, ncol=2, scales="free_y") + guides(color = "none") + labs(x = "Point Margin", y = NULL) + theme(axis.text=element_text(size=8)) ``` ] .panel2-reveal-elecfacet1-auto[ <img src="09a-slides_files/figure-html/reveal-elecfacet1_auto_13_output-1.png" width="504" style="display: block; margin: auto;" /> ] <style> .panel1-reveal-elecfacet1-auto { color: black; width: 44.1%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-reveal-elecfacet1-auto { color: black; width: 53.9%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-reveal-elecfacet1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- layout: true class: title title-1 --- # With a bit more room .pull-left.w60[ <img src="09a-slides_files/figure-html/07-make-maps-4-1.png" width="576" style="display: block; margin: auto;" /> ] -- .pull-right.w40[ - See how the panels are unbalanced, even with `scales = "free_y"`? - This happens because we have unequal number of states per region. ] --- # We can use .kjh-green[`facet_col()`] from .kjh-lblue[`ggforce`] .pull-left.w45[ ```r p_out <- election |> filter(st %nin% "DC") |> ggplot(mapping = aes(x = r_points, y = reorder(state, r_points), color = party)) + geom_vline(xintercept = 0, color = "gray30") + geom_point(size = 2) + scale_color_manual(values = party_colors) + scale_x_continuous(breaks = c(-30, -20, -10, 0, 10, 20, 30, 40), labels = c("30\n (Clinton)", "20", "10", "0", "10", "20", "30", "40\n(Trump)")) + * facet_col(~ census, * scales="free_y", * space = "free") + guides(color = "none") + labs(x = "Point Margin", y = NULL) + theme(axis.text=element_text(size=6), strip.text = element_text(size = rel(0.6))) p_out ``` ] -- .pull-right.w55[ <img src="09a-slides_files/figure-html/codefig-ggforce-1.png" width="240" style="display: block; margin: auto;" /> ]