class: center, middle, inverse, title-slide # Making Maps with R ### Courtney Lee ### Public Policy Institute of California ### 2019-10-16 --- class: inverse, middle, left # What do we need before building our maps? <!-- * shapefiles? --> * working knowledge of R -- * the data we want to visualize -- * an idea of what we want as our result --- class: inverse, middle ## A Glimpse of the Data ``` ## # A tibble: 10 x 3 ## ctemacro unwgt_n eduattain_aa_pct ## <chr> <dbl> <dbl> ## 1 Northern 376. 0.566 ## 2 Sacramento Area 651. 0.546 ## 3 SF Bay Area 1222. 0.518 ## 4 Santa Clara/Santa Cruz/Monterey 469. 0.465 ## 5 Central Valley/Mother Lode 1059. 0.623 ## 6 Central Coast 396. 0.552 ## 7 Los Angeles 2140. 0.482 ## 8 Inland Empire 1186. 0.555 ## 9 Orange County 541. 0.400 ## 10 San Diego/Imperial 810. 0.529 ``` --- class: inverse, middle ### One way to visualize the data <img src="makemapswithR_slidedeck_191015_files/figure-html/occ33-barchart-1.png" width="720" style="display: block; margin: auto;" /> --- class: inverse, middle ### Map version <img src="./makemapswithR_slidedeck_191015_files/figure-html/occ33-viridis-1.png" width="70%" height="70%" style="display: block; margin: auto;" /> --- class: inverse, middle # Libraries we need ```r library(ggplot2) #plotting engine library(maps) #contains outlines library(mapdata) #higher resolution outlines library(mapproj) #map projections collection ``` --- class: inverse, middle # USA, as a country, in a dataframe ```r usa <- map_data("usa") dim(usa) ``` ``` # [1] 7243 6 ``` ```r head(usa) ``` ``` # long lat group order region subregion # 1 -101.4078 29.74224 1 1 main <NA> # 2 -101.3906 29.74224 1 2 main <NA> # 3 -101.3620 29.65056 1 3 main <NA> # 4 -101.3505 29.63911 1 4 main <NA> # 5 -101.3219 29.63338 1 5 main <NA> # 6 -101.3047 29.64484 1 6 main <NA> ``` --- class: inverse, middle # USA, dataframe, visualized .pull-left[ ```r library(ggplot2) usa <- map_data("usa") usa_base <- ggplot() + geom_polygon(data = usa, aes(x = long, y = lat, group = group )) + coord_fixed(1.3) usa_base ``` ] .pull-right[ <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-4-1.png" width="504" style="display: block; margin: auto;" /> ] --- class: inverse, middle ### Change the fill color and projection ```r library(ggplot2) usa <- map_data("usa") ggplot() + geom_polygon(data = usa, aes(x = long, y = lat, group = group), * fill = "lightgreen", color = "darkgreen") + coord_fixed(1.3) + labs(title = "The 48 Contiguous States of the USA") + * coord_map("conic", lat0 = 30) + theme(panel.background = element_blank(), axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank()) ``` --- class: inverse, middle ### Fixed colors and projections <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-5-1.png" width="504" style="display: block; margin: auto;" /> --- class: inverse, middle # County Levels ```r states <- map_data("state") calif <- subset(states, region == "california") # just the lat long points for the state # county lines *ca_counties <- subset(map_data("county"), region == "california") # plot the state itself ca_base <- ggplot(data = calif, mapping = aes(x = long, y = lat, group = group)) + coord_fixed(1.3) + geom_polygon(color = "black", fill = "lightgreen") + theme(panel.background = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), panel.border = element_blank(), panel.grid = element_blank()) # plot county lines ca_base + geom_polygon(data = ca_counties, fill = NA, color = "white") + geom_polygon(color = "black", fill = NA) ``` ```r # get state border drawn as top layer ``` --- class: inverse, middle ### All 58 of California's counties <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-6-1.png" width="504" style="display: block; margin: auto;" /> --- class: inverse, middle ### Forming County Groups ```r # attach our county group definition to the lat/longs # provided by the `maps` library *cts_macro <- inner_join(x = counties, y = cty_macro) ca_base + geom_polygon(data = cts_macro, aes(fill = msaname), color = "white") + geom_polygon(color = "black", fill = NA) + scale_fill_viridis_d(option = "C") + labs(fill = "MSA Regions") + guides(fill = FALSE) ``` --- class: inverse, middle ## 34 Metropolitan Statistical Areas <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-7-1.png" width="504" style="display: block; margin: auto;" /> Metropolitan Statistical Areas, according to US Bureau of Labor Statistics --- class: inverse, middle ### Another Definition of County Groups: Fifteen Groups (formed by attaching this 15 county group definition to the dataframe found in `map_data::counties`) -- <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-8-1.png" width="504" style="display: block; margin: auto;" /> Source: California Community Colleges Chancellor's Office --- class: inverse, middle ### Consolidating 34 MSAs and 15 CC Regions Together -- ... into ten regions! Now we are able to talk about regional employment demand (via BLS) and supply (how community colleges could address). -- <!-- ### Ten County Groups --> <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-9-1.png" width="504" style="display: block; margin: auto;" /> --- class: inverse, middle ### Which regions sees the greatest share of Californians with some college educational attainment, and are employed in the public and protective services sector? <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-10-1.png" width="504" style="display: block; margin: auto;" /> --- class: inverse, middle ### Change that color scheme ```r # 33. public and protective services occ33 <- occ2sr %>% filter(occsoc_2 == "33" & ctemacro != "Statewide") cty_occ33 <- inner_join(x = cts_macro, y = occ33, by = c("ctemacro" = "ctemacro")) ca_base + geom_polygon(data = cty_occ33, aes(fill = eduattain_aa_pct), color = "white") + * scale_fill_distiller(type = "div", direction = 1, labels = scales::percent_format(accuracy = 1), ) + geom_polygon(color = "black", fill = NA) + #state outline labs(fill = "Percent Some College\nAttainment") ``` --- class: inverse, middle ### Change that color scheme <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-11-1.png" width="504" style="display: block; margin: auto;" /> --- class: inverse, middle ### Change that color scheme, again ```r # 33. public and protective services occ33 <- occ2sr %>% filter(occsoc_2 == "33" & ctemacro != "Statewide") cty_occ33 <- inner_join(x = cts_macro, y = occ33, by = c("ctemacro" = "ctemacro")) ca_base + geom_polygon(data = cty_occ33, aes(fill = eduattain_aa_pct), color = "white") + * scale_fill_viridis_c(option = "D", labels = scales::percent_format(accuracy = 1), ) + geom_polygon(color = "black", fill = NA) + #state outline labs(fill = "Percent Some College\nAttainment") ``` --- class: inverse, middle ### Change that color scheme, again <img src="makemapswithR_slidedeck_191015_files/figure-html/unnamed-chunk-12-1.png" width="504" style="display: block; margin: auto;" /> --- class: inverse, middle, left # Thanks! Content adapted from [Reproducible Research/Making Maps with R](https://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html). Slides are available on **GitHub**: [https://www.github.com/leecourt98/nacis-2019/](https://www.github.com/leecourt98/nacis-2019/). Slides created via the R package [xaringan](https://github.com/yihui/xaringan). <!-- class: inverse, center, middle --> <!-- # What this slide deck covers --> <!-- ## YES --> <!-- rapid prototyping for static maps, with R --> <!-- ## NOPE --> <!-- leaflet, javascript, etc --> <!-- --- --> <!-- class: inverse, middle, left --> <!-- ## Qualities I like about R's graphics --> <!-- - Working in the same environment where we wrangled our data! --> <!-- -- --> <!-- - Super customizable (especially with layers) --> <!-- -- adding a layer is easy, changing a feature is doable! --> <!-- -- --> <!-- - Reproducibility --> <!-- -- I can reproduce my work at a later date and follow it! --> <!-- ```{r, echo=FALSE} --> <!-- knitr::include_graphics(path = "./slide deck gfx/msa_map_190123.png") --> <!-- # to add: --> <!-- # join dataframe of MSA identifiers with counties from `map_data` --> <!-- <!-- # no shapefile needed, our geography units are in `map_data` --> --> <!-- ```{r, echo=FALSE, out.height="60%", out.width="60%"} --> <!-- knitr::include_graphics("./slide deck gfx/cteregions_defn_190122.png") --> <!-- # to add: --> <!-- # here's another type of county groups --> <!-- ``` --> <!-- ```{r, echo=FALSE, out.height="60%", out.width="60%"} --> <!-- knitr::include_graphics(path = "./slide deck gfx/ctemacro_defn_190122.png") --> <!-- # to add: --> <!-- # here's a collection of county groups used in a report. --> <!-- ``` --> <!-- ``` -->