class: center middle main-title section-title-1 # .kjh-yellow[Maps and]<br /> .kjh-lblue[Spatial Data] .class-info[ **Week 09 (Part 2)** .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 ``` ``` ## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE ``` ```r 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[Simple Features]] --- layout: true class: title title-1 --- # .kjh-green[`geom_polygon()`] is limiting - It's very useful to have the intuition that, when drawing maps, .kjh-orange[we're just working with tables] of `x` and `y` coordinates, and .kjh-orange[shapes represent quantities in our data], in a way that's essentially the same as any other geom. This makes it worth getting comfortable with what .kjh-green[`geom_polygon()`] and .kjh-green[`coord_map()`] are doing. But the business of having very large map tables and manually specifying projections is inefficient. -- - In addition, sometimes our data _really is_ properly spatial, at which point we need a more rigorous and consistent way of specifying those elements. There's a whole world of Geodesic standards and methods devoted to specifying these things for GIS applications. R is not a dedicated GIS, but we can take advantage of these tools. -- - .center.large[Enter .kjh-pink[simple features], the .kjh-lblue[`sf`] package, and .kjh-green[`geom_sf()`]] --- # The Simple Features package - When we load .kjh-lblue[`sf`] it creates a way to use several standard GIS concepts and tools, such as the .kjh-lblue[GEOS] library for computational geometry, the .kjh-lblue[PROJ] software that transforms spatial coordinates from one reference system to another, as in map projections, and the Simple Features standard for specifying the elements of spatial attributes. ```r library(sf) ``` ``` ## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE ``` -- Let's see the main upshot for us as end-users. --- # The .kjh-lblue[`nycdogs`] package .pull-left.w60[ ```r library(nycdogs) nyc_license ``` ``` ## # A tibble: 493,072 × 9 ## anima…¹ anima…² anima…³ breed…⁴ borough zip_c…⁵ license_…⁶ license_…⁷ extra…⁸ ## <chr> <chr> <dbl> <chr> <chr> <int> <date> <date> <dbl> ## 1 Paige F 2014 Pit Bu… Manhat… 10035 2014-09-12 2017-09-12 2016 ## 2 Yogi M 2010 Boxer Bronx 10465 2014-09-12 2017-10-02 2016 ## 3 Ali M 2014 Basenji Manhat… 10013 2014-09-12 2019-09-12 2016 ## 4 Queen F 2013 Akita … Manhat… 10013 2014-09-12 2017-09-12 2016 ## 5 Lola F 2009 Maltese Manhat… 10028 2014-09-12 2017-10-09 2016 ## 6 Ian M 2006 Unknown Manhat… 10013 2014-09-12 2019-10-30 2016 ## 7 Buddy M 2008 Unknown Manhat… 10025 2014-09-12 2017-10-20 2016 ## 8 Chewba… F 2012 Labrad… Manhat… 10013 2014-09-12 2019-10-01 2016 ## 9 Heidi-… F 2007 Dachsh… Brookl… 11215 2014-09-13 2017-04-16 2016 ## 10 Massimo M 2009 Bull D… Brookl… 11201 2014-09-13 2017-09-17 2016 ## # … with 493,062 more rows, and abbreviated variable names ¹animal_name, ## # ²animal_gender, ³animal_birth_year, ⁴breed_rc, ⁵zip_code, ## # ⁶license_issued_date, ⁷license_expired_date, ⁸extract_year ``` ] .pull-right.w40[ .center[] ] --- # The .kjh-lblue[`nycdogs`] package - The metadata tells you this is not a regular tibble. ```r nyc_zips ``` ``` ## Simple feature collection with 262 features and 11 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 12 ## objec…¹ zip_c…² po_name state borough st_fips cty_f…³ bld_g…⁴ shape…⁵ shape…⁶ ## <int> <int> <chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> ## 1 1 11372 Jackso… NY Queens 36 081 0 20625. 2.02e7 ## 2 2 11004 Glen O… NY Queens 36 081 0 23003. 2.26e7 ## 3 3 11040 New Hy… NY Queens 36 081 0 15749. 6.27e6 ## 4 4 11426 Beller… NY Queens 36 081 0 35933. 4.94e7 ## 5 5 11365 Fresh … NY Queens 36 081 0 38694. 6.94e7 ## 6 6 11373 Elmhur… NY Queens 36 081 0 33756. 4.27e7 ## 7 7 11001 Floral… NY Queens 36 081 0 13595. 9.16e6 ## 8 8 11375 Forest… NY Queens 36 081 0 36277. 5.56e7 ## 9 9 11427 Queens… NY Queens 36 081 0 31232. 3.96e7 ## 10 10 11374 Rego P… NY Queens 36 081 0 26324. 2.52e7 ## # … with 252 more rows, 2 more variables: x_id <chr>, geometry <POLYGON [°]>, ## # and abbreviated variable names ¹objectid, ²zip_code, ³cty_fips, ## # ⁴bld_gpostal_code, ⁵shape_leng, ⁶shape_area ``` --- # The .kjh-lblue[`nycdogs`] package ```r nyc_zips |> select(objectid:borough) ``` ``` ## Simple feature collection with 262 features and 5 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 6 ## objectid zip_code po_name state borough geometry ## <int> <int> <chr> <chr> <chr> <POLYGON [°]> ## 1 1 11372 Jackson Heights NY Queens ((-73.86942 40.74916, -73.89… ## 2 2 11004 Glen Oaks NY Queens ((-73.71068 40.75004, -73.70… ## 3 3 11040 New Hyde Park NY Queens ((-73.70098 40.7389, -73.703… ## 4 4 11426 Bellerose NY Queens ((-73.7227 40.75373, -73.722… ## 5 5 11365 Fresh Meadows NY Queens ((-73.81089 40.72717, -73.81… ## 6 6 11373 Elmhurst NY Queens ((-73.88722 40.72753, -73.88… ## 7 7 11001 Floral Park NY Queens ((-73.70098 40.7389, -73.699… ## 8 8 11375 Forest Hills NY Queens ((-73.85625 40.73672, -73.85… ## 9 9 11427 Queens Village NY Queens ((-73.74169 40.73682, -73.73… ## 10 10 11374 Rego Park NY Queens ((-73.86451 40.73407, -73.85… ## # … with 252 more rows ``` - The .kjh-orange[`polygon`] column is a list of lat/lon points that, when joined, draw the outline of the zip code area. This is _much_ more compact than a big table where every row is a single point. --- # Let's make a summary table --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r *nyc_license ``` ] .panel2-reveal-nycfrench-auto[ ``` ## # A tibble: 493,072 × 9 ## anima…¹ anima…² anima…³ breed…⁴ borough zip_c…⁵ license_…⁶ license_…⁷ extra…⁸ ## <chr> <chr> <dbl> <chr> <chr> <int> <date> <date> <dbl> ## 1 Paige F 2014 Pit Bu… Manhat… 10035 2014-09-12 2017-09-12 2016 ## 2 Yogi M 2010 Boxer Bronx 10465 2014-09-12 2017-10-02 2016 ## 3 Ali M 2014 Basenji Manhat… 10013 2014-09-12 2019-09-12 2016 ## 4 Queen F 2013 Akita … Manhat… 10013 2014-09-12 2017-09-12 2016 ## 5 Lola F 2009 Maltese Manhat… 10028 2014-09-12 2017-10-09 2016 ## 6 Ian M 2006 Unknown Manhat… 10013 2014-09-12 2019-10-30 2016 ## 7 Buddy M 2008 Unknown Manhat… 10025 2014-09-12 2017-10-20 2016 ## 8 Chewba… F 2012 Labrad… Manhat… 10013 2014-09-12 2019-10-01 2016 ## 9 Heidi-… F 2007 Dachsh… Brookl… 11215 2014-09-13 2017-04-16 2016 ## 10 Massimo M 2009 Bull D… Brookl… 11201 2014-09-13 2017-09-17 2016 ## # … with 493,062 more rows, and abbreviated variable names ¹animal_name, ## # ²animal_gender, ³animal_birth_year, ⁴breed_rc, ⁵zip_code, ## # ⁶license_issued_date, ⁷license_expired_date, ⁸extract_year ``` ] --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r nyc_license |> * filter(extract_year == 2018) ``` ] .panel2-reveal-nycfrench-auto[ ``` ## # A tibble: 117,371 × 9 ## anima…¹ anima…² anima…³ breed…⁴ borough zip_c…⁵ license_…⁶ license_…⁷ extra…⁸ ## <chr> <chr> <dbl> <chr> <chr> <int> <date> <date> <dbl> ## 1 Ali M 2014 Basenji Manhat… 10013 2014-09-12 2019-09-12 2018 ## 2 Ian M 2006 Unknown Manhat… 10013 2014-09-12 2019-10-30 2018 ## 3 Chewba… F 2012 Labrad… Manhat… 10013 2014-09-12 2019-10-01 2018 ## 4 Lola F 2006 Miniat… Manhat… 10022 2014-09-13 2019-10-03 2018 ## 5 Lucy F 2014 Dachsh… Brookl… 11215 2014-09-13 2019-09-13 2018 ## 6 June F 2010 Cavali… Brookl… 11238 2014-09-13 2019-10-28 2018 ## 7 Apple M 2013 Havane… Manhat… 10025 2014-09-13 2019-10-16 2018 ## 8 Muneca F 2013 Beagle Brookl… 11232 2014-09-13 2019-09-13 2018 ## 9 Benson M 2010 Boxer Brookl… 11209 2014-09-13 2019-10-29 2018 ## 10 Bigs M 2004 Pit Bu… Brookl… 11208 2014-09-13 2019-10-27 2018 ## # … with 117,361 more rows, and abbreviated variable names ¹animal_name, ## # ²animal_gender, ³animal_birth_year, ⁴breed_rc, ⁵zip_code, ## # ⁶license_issued_date, ⁷license_expired_date, ⁸extract_year ``` ] --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r nyc_license |> filter(extract_year == 2018) |> * group_by(breed_rc, zip_code) ``` ] .panel2-reveal-nycfrench-auto[ ``` ## # A tibble: 117,371 × 9 ## # Groups: breed_rc, zip_code [18,945] ## anima…¹ anima…² anima…³ breed…⁴ borough zip_c…⁵ license_…⁶ license_…⁷ extra…⁸ ## <chr> <chr> <dbl> <chr> <chr> <int> <date> <date> <dbl> ## 1 Ali M 2014 Basenji Manhat… 10013 2014-09-12 2019-09-12 2018 ## 2 Ian M 2006 Unknown Manhat… 10013 2014-09-12 2019-10-30 2018 ## 3 Chewba… F 2012 Labrad… Manhat… 10013 2014-09-12 2019-10-01 2018 ## 4 Lola F 2006 Miniat… Manhat… 10022 2014-09-13 2019-10-03 2018 ## 5 Lucy F 2014 Dachsh… Brookl… 11215 2014-09-13 2019-09-13 2018 ## 6 June F 2010 Cavali… Brookl… 11238 2014-09-13 2019-10-28 2018 ## 7 Apple M 2013 Havane… Manhat… 10025 2014-09-13 2019-10-16 2018 ## 8 Muneca F 2013 Beagle Brookl… 11232 2014-09-13 2019-09-13 2018 ## 9 Benson M 2010 Boxer Brookl… 11209 2014-09-13 2019-10-29 2018 ## 10 Bigs M 2004 Pit Bu… Brookl… 11208 2014-09-13 2019-10-27 2018 ## # … with 117,361 more rows, and abbreviated variable names ¹animal_name, ## # ²animal_gender, ³animal_birth_year, ⁴breed_rc, ⁵zip_code, ## # ⁶license_issued_date, ⁷license_expired_date, ⁸extract_year ``` ] --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> * tally() ``` ] .panel2-reveal-nycfrench-auto[ ``` ## # A tibble: 18,945 × 3 ## # Groups: breed_rc [311] ## breed_rc zip_code n ## <chr> <int> <int> ## 1 Affenpinscher 10005 1 ## 2 Affenpinscher 10011 1 ## 3 Affenpinscher 10013 1 ## 4 Affenpinscher 10014 1 ## 5 Affenpinscher 10016 1 ## 6 Affenpinscher 10017 1 ## 7 Affenpinscher 10018 1 ## 8 Affenpinscher 10019 1 ## 9 Affenpinscher 10021 1 ## 10 Affenpinscher 10023 1 ## # … with 18,935 more rows ``` ] --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> * mutate(freq = n / sum(n)) ``` ] .panel2-reveal-nycfrench-auto[ ``` ## # A tibble: 18,945 × 4 ## # Groups: breed_rc [311] ## breed_rc zip_code n freq ## <chr> <int> <int> <dbl> ## 1 Affenpinscher 10005 1 0.0303 ## 2 Affenpinscher 10011 1 0.0303 ## 3 Affenpinscher 10013 1 0.0303 ## 4 Affenpinscher 10014 1 0.0303 ## 5 Affenpinscher 10016 1 0.0303 ## 6 Affenpinscher 10017 1 0.0303 ## 7 Affenpinscher 10018 1 0.0303 ## 8 Affenpinscher 10019 1 0.0303 ## 9 Affenpinscher 10021 1 0.0303 ## 10 Affenpinscher 10023 1 0.0303 ## # … with 18,935 more rows ``` ] --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> mutate(freq = n / sum(n)) |> * filter(breed_rc == "French Bulldog") ``` ] .panel2-reveal-nycfrench-auto[ ``` ## # A tibble: 161 × 4 ## # Groups: breed_rc [1] ## breed_rc zip_code n freq ## <chr> <int> <int> <dbl> ## 1 French Bulldog 10001 27 0.0167 ## 2 French Bulldog 10002 20 0.0123 ## 3 French Bulldog 10003 36 0.0222 ## 4 French Bulldog 10004 9 0.00555 ## 5 French Bulldog 10005 15 0.00925 ## 6 French Bulldog 10006 8 0.00494 ## 7 French Bulldog 10007 17 0.0105 ## 8 French Bulldog 10009 51 0.0315 ## 9 French Bulldog 10010 31 0.0191 ## 10 French Bulldog 10011 88 0.0543 ## # … with 151 more rows ``` ] --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> mutate(freq = n / sum(n)) |> filter(breed_rc == "French Bulldog") -> * nyc_fb ``` ] .panel2-reveal-nycfrench-auto[ ] --- count: false # Let's make a summary table .panel1-reveal-nycfrench-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> mutate(freq = n / sum(n)) |> filter(breed_rc == "French Bulldog") -> nyc_fb ``` ] .panel2-reveal-nycfrench-auto[ ] <style> .panel1-reveal-nycfrench-auto { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-reveal-nycfrench-auto { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-reveal-nycfrench-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Now we have two tables again .pull-left.w70[ ```r nyc_zips |> select(objectid:st_fips) ``` ``` ## Simple feature collection with 262 features and 6 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 7 ## objectid zip_code po_name state borough st_fips geometry ## <int> <int> <chr> <chr> <chr> <chr> <POLYGON [°]> ## 1 1 11372 Jackson He… NY Queens 36 ((-73.86942 40.74916, -7… ## 2 2 11004 Glen Oaks NY Queens 36 ((-73.71068 40.75004, -7… ## 3 3 11040 New Hyde P… NY Queens 36 ((-73.70098 40.7389, -73… ## 4 4 11426 Bellerose NY Queens 36 ((-73.7227 40.75373, -73… ## 5 5 11365 Fresh Mead… NY Queens 36 ((-73.81089 40.72717, -7… ## 6 6 11373 Elmhurst NY Queens 36 ((-73.88722 40.72753, -7… ## 7 7 11001 Floral Park NY Queens 36 ((-73.70098 40.7389, -73… ## 8 8 11375 Forest Hil… NY Queens 36 ((-73.85625 40.73672, -7… ## 9 9 11427 Queens Vil… NY Queens 36 ((-73.74169 40.73682, -7… ## 10 10 11374 Rego Park NY Queens 36 ((-73.86451 40.73407, -7… ## # … with 252 more rows ``` ] -- .pull-right.w30[ ```r nyc_fb |> select(breed_rc:n) ``` ``` ## # A tibble: 161 × 3 ## # Groups: breed_rc [1] ## breed_rc zip_code n ## <chr> <int> <int> ## 1 French Bulldog 10001 27 ## 2 French Bulldog 10002 20 ## 3 French Bulldog 10003 36 ## 4 French Bulldog 10004 9 ## 5 French Bulldog 10005 15 ## 6 French Bulldog 10006 8 ## 7 French Bulldog 10007 17 ## 8 French Bulldog 10009 51 ## 9 French Bulldog 10010 31 ## 10 French Bulldog 10011 88 ## # … with 151 more rows ``` ] -- - Join them: ```r fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") ``` --- # Ready to map ```r fb_map |> select(zip_code, po_name, borough, breed_rc:freq, geometry) ``` ``` ## Simple feature collection with 262 features and 6 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 7 ## zip_code po_name borough breed…¹ n freq geometry ## <int> <chr> <chr> <chr> <int> <dbl> <POLYGON [°]> ## 1 11372 Jackson He… Queens French… 13 8.02e-3 ((-73.86942 40.74916, -7… ## 2 11004 Glen Oaks Queens French… 1 6.17e-4 ((-73.71068 40.75004, -7… ## 3 11040 New Hyde P… Queens <NA> NA NA ((-73.70098 40.7389, -73… ## 4 11426 Bellerose Queens French… 1 6.17e-4 ((-73.7227 40.75373, -73… ## 5 11365 Fresh Mead… Queens French… 7 4.32e-3 ((-73.81089 40.72717, -7… ## 6 11373 Elmhurst Queens French… 14 8.64e-3 ((-73.88722 40.72753, -7… ## 7 11001 Floral Park Queens <NA> NA NA ((-73.70098 40.7389, -73… ## 8 11375 Forest Hil… Queens French… 8 4.94e-3 ((-73.85625 40.73672, -7… ## 9 11427 Queens Vil… Queens French… 2 1.23e-3 ((-73.74169 40.73682, -7… ## 10 11374 Rego Park Queens French… 6 3.70e-3 ((-73.86451 40.73407, -7… ## # … with 252 more rows, and abbreviated variable name ¹breed_rc ``` --- # A NYC map theme - Just moving the legend, really. ```r theme_nymap <- function(base_size=9, base_family="") { require(grid) theme_bw(base_size=base_size, base_family=base_family) %+replace% theme(axis.line=element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank(), panel.background=element_blank(), panel.border=element_blank(), panel.grid=element_blank(), panel.spacing=unit(0, "lines"), plot.background=element_blank(), legend.justification = c(0,0), legend.position = c(0.05, 0.58), legend.direction = "horizontal" ) } ``` --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes *load("fb_map.Rdata") ``` ] .panel2-reveal-bulldogmap-auto[ ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # *fb_map ``` ] .panel2-reveal-bulldogmap-auto[ ``` ## Simple feature collection with 262 features and 14 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 15 ## objec…¹ zip_c…² po_name state borough st_fips cty_f…³ bld_g…⁴ shape…⁵ shape…⁶ ## <int> <int> <chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> ## 1 1 11372 Jackso… NY Queens 36 081 0 20625. 2.02e7 ## 2 2 11004 Glen O… NY Queens 36 081 0 23003. 2.26e7 ## 3 3 11040 New Hy… NY Queens 36 081 0 15749. 6.27e6 ## 4 4 11426 Beller… NY Queens 36 081 0 35933. 4.94e7 ## 5 5 11365 Fresh … NY Queens 36 081 0 38694. 6.94e7 ## 6 6 11373 Elmhur… NY Queens 36 081 0 33756. 4.27e7 ## 7 7 11001 Floral… NY Queens 36 081 0 13595. 9.16e6 ## 8 8 11375 Forest… NY Queens 36 081 0 36277. 5.56e7 ## 9 9 11427 Queens… NY Queens 36 081 0 31232. 3.96e7 ## 10 10 11374 Rego P… NY Queens 36 081 0 26324. 2.52e7 ## # … with 252 more rows, 5 more variables: x_id <chr>, geometry <POLYGON [°]>, ## # breed_rc <chr>, n <int>, freq <dbl>, and abbreviated variable names ## # ¹objectid, ²zip_code, ³cty_fips, ⁴bld_gpostal_code, ⁵shape_leng, ## # ⁶shape_area ``` ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> * ggplot(mapping = aes(fill = freq)) ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_03_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) #<< ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_04_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) + * scale_fill_viridis_c(option = "A") ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_05_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) + scale_fill_viridis_c(option = "A") + * labs(fill = "Percent of All French Bulldogs") ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_06_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) + scale_fill_viridis_c(option = "A") + labs(fill = "Percent of All French Bulldogs") + * annotate(geom = "text", * x = -74.145 + 0.029, * y = 40.82-0.012, * label = "New York City's French Bulldogs", * size = 6) ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_07_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) + scale_fill_viridis_c(option = "A") + labs(fill = "Percent of All French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + * annotate(geom = "text", * x = -74.1468 + 0.029, * y = 40.8075-0.012, * label = "By Zip Code. Based on Licensing Data", * size = 5) ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_08_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) + scale_fill_viridis_c(option = "A") + labs(fill = "Percent of All French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.8075-0.012, label = "By Zip Code. Based on Licensing Data", size = 5) + * kjhslides::kjh_theme_nymap() ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_09_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) + scale_fill_viridis_c(option = "A") + labs(fill = "Percent of All French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.8075-0.012, label = "By Zip Code. Based on Licensing Data", size = 5) + kjhslides::kjh_theme_nymap() + * guides(fill = * guide_legend(title.position = "top", * label.position = "bottom", * keywidth = 1, * nrow = 1)) ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_10_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # First cut at a map .panel1-reveal-bulldogmap-auto[ ```r # This is just for slide purposes load("fb_map.Rdata") # fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) + scale_fill_viridis_c(option = "A") + labs(fill = "Percent of All French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.8075-0.012, label = "By Zip Code. Based on Licensing Data", size = 5) + kjhslides::kjh_theme_nymap() + guides(fill = guide_legend(title.position = "top", label.position = "bottom", keywidth = 1, nrow = 1)) ``` ] .panel2-reveal-bulldogmap-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap_auto_11_output-1.png" width="504" style="display: block; margin: auto;" /> ] <style> .panel1-reveal-bulldogmap-auto { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-reveal-bulldogmap-auto { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-reveal-bulldogmap-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes *load("nyc_fb.Rdata") ``` ] .panel2-reveal-bulldogmap2-auto[ ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") *library(colorspace) ``` ] .panel2-reveal-bulldogmap2-auto[ ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) *fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") ``` ] .panel2-reveal-bulldogmap2-auto[ ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") *fb_map ``` ] .panel2-reveal-bulldogmap2-auto[ ``` ## Simple feature collection with 262 features and 14 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 15 ## objec…¹ zip_c…² po_name state borough st_fips cty_f…³ bld_g…⁴ shape…⁵ shape…⁶ ## <int> <int> <chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> ## 1 1 11372 Jackso… NY Queens 36 081 0 20625. 2.02e7 ## 2 2 11004 Glen O… NY Queens 36 081 0 23003. 2.26e7 ## 3 3 11040 New Hy… NY Queens 36 081 0 15749. 6.27e6 ## 4 4 11426 Beller… NY Queens 36 081 0 35933. 4.94e7 ## 5 5 11365 Fresh … NY Queens 36 081 0 38694. 6.94e7 ## 6 6 11373 Elmhur… NY Queens 36 081 0 33756. 4.27e7 ## 7 7 11001 Floral… NY Queens 36 081 0 13595. 9.16e6 ## 8 8 11375 Forest… NY Queens 36 081 0 36277. 5.56e7 ## 9 9 11427 Queens… NY Queens 36 081 0 31232. 3.96e7 ## 10 10 11374 Rego P… NY Queens 36 081 0 26324. 2.52e7 ## # … with 252 more rows, 5 more variables: x_id <chr>, geometry <POLYGON [°]>, ## # breed_rc <chr>, n <int>, freq <dbl>, and abbreviated variable names ## # ¹objectid, ²zip_code, ³cty_fips, ⁴bld_gpostal_code, ⁵shape_leng, ## # ⁶shape_area ``` ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> * ggplot(mapping = aes(fill = freq)) ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_05_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_06_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + * scale_fill_continuous_sequential( * palette = "Oranges", * labels = scales::label_percent()) ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_07_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + * labs(fill = "Percent of all French Bulldogs") ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_08_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + * annotate(geom = "text", * x = -74.145 + 0.029, * y = 40.82-0.012, * label = "New York City's French Bulldogs", * size = 6) ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_09_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + * annotate(geom = "text", * x = -74.1468 + 0.029, * y = 40.7955, * label = "By Zip Code. Based on Licensing Data", * size = 5) ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_10_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.7955, label = "By Zip Code. Based on Licensing Data", size = 5) + * kjhslides::kjh_theme_nymap() ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_11_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.7955, label = "By Zip Code. Based on Licensing Data", size = 5) + kjhslides::kjh_theme_nymap() + * guides(fill = * guide_legend(title.position = "top", * label.position = "bottom", * keywidth = 1, * nrow = 1)) ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_12_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Use a different palette .panel1-reveal-bulldogmap2-auto[ ```r # This is just for slide purposes load("nyc_fb.Rdata") library(colorspace) fb_map <- left_join(nyc_zips, nyc_fb, by = "zip_code") fb_map |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.7955, label = "By Zip Code. Based on Licensing Data", size = 5) + kjhslides::kjh_theme_nymap() + guides(fill = guide_legend(title.position = "top", label.position = "bottom", keywidth = 1, nrow = 1)) ``` ] .panel2-reveal-bulldogmap2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogmap2_auto_13_output-1.png" width="504" style="display: block; margin: auto;" /> ] <style> .panel1-reveal-bulldogmap2-auto { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-reveal-bulldogmap2-auto { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-reveal-bulldogmap2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- layout: false <img src="09b-slides_files/figure-html/07-make-maps-59-1.png" width="864" style="display: block; margin: auto;" /> .right.w90.small[NYC Dogs Map mark 2] --- layout: true class: title title-1 --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r *nyc_license ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 493,072 × 9 ## anima…¹ anima…² anima…³ breed…⁴ borough zip_c…⁵ license_…⁶ license_…⁷ extra…⁸ ## <chr> <chr> <dbl> <chr> <chr> <int> <date> <date> <dbl> ## 1 Paige F 2014 Pit Bu… Manhat… 10035 2014-09-12 2017-09-12 2016 ## 2 Yogi M 2010 Boxer Bronx 10465 2014-09-12 2017-10-02 2016 ## 3 Ali M 2014 Basenji Manhat… 10013 2014-09-12 2019-09-12 2016 ## 4 Queen F 2013 Akita … Manhat… 10013 2014-09-12 2017-09-12 2016 ## 5 Lola F 2009 Maltese Manhat… 10028 2014-09-12 2017-10-09 2016 ## 6 Ian M 2006 Unknown Manhat… 10013 2014-09-12 2019-10-30 2016 ## 7 Buddy M 2008 Unknown Manhat… 10025 2014-09-12 2017-10-20 2016 ## 8 Chewba… F 2012 Labrad… Manhat… 10013 2014-09-12 2019-10-01 2016 ## 9 Heidi-… F 2007 Dachsh… Brookl… 11215 2014-09-13 2017-04-16 2016 ## 10 Massimo M 2009 Bull D… Brookl… 11201 2014-09-13 2017-09-17 2016 ## # … with 493,062 more rows, and abbreviated variable names ¹animal_name, ## # ²animal_gender, ³animal_birth_year, ⁴breed_rc, ⁵zip_code, ## # ⁶license_issued_date, ⁷license_expired_date, ⁸extract_year ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> * filter(extract_year == 2018) ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 117,371 × 9 ## anima…¹ anima…² anima…³ breed…⁴ borough zip_c…⁵ license_…⁶ license_…⁷ extra…⁸ ## <chr> <chr> <dbl> <chr> <chr> <int> <date> <date> <dbl> ## 1 Ali M 2014 Basenji Manhat… 10013 2014-09-12 2019-09-12 2018 ## 2 Ian M 2006 Unknown Manhat… 10013 2014-09-12 2019-10-30 2018 ## 3 Chewba… F 2012 Labrad… Manhat… 10013 2014-09-12 2019-10-01 2018 ## 4 Lola F 2006 Miniat… Manhat… 10022 2014-09-13 2019-10-03 2018 ## 5 Lucy F 2014 Dachsh… Brookl… 11215 2014-09-13 2019-09-13 2018 ## 6 June F 2010 Cavali… Brookl… 11238 2014-09-13 2019-10-28 2018 ## 7 Apple M 2013 Havane… Manhat… 10025 2014-09-13 2019-10-16 2018 ## 8 Muneca F 2013 Beagle Brookl… 11232 2014-09-13 2019-09-13 2018 ## 9 Benson M 2010 Boxer Brookl… 11209 2014-09-13 2019-10-29 2018 ## 10 Bigs M 2004 Pit Bu… Brookl… 11208 2014-09-13 2019-10-27 2018 ## # … with 117,361 more rows, and abbreviated variable names ¹animal_name, ## # ²animal_gender, ³animal_birth_year, ⁴breed_rc, ⁵zip_code, ## # ⁶license_issued_date, ⁷license_expired_date, ⁸extract_year ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> * group_by(breed_rc, zip_code) ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 117,371 × 9 ## # Groups: breed_rc, zip_code [18,945] ## anima…¹ anima…² anima…³ breed…⁴ borough zip_c…⁵ license_…⁶ license_…⁷ extra…⁸ ## <chr> <chr> <dbl> <chr> <chr> <int> <date> <date> <dbl> ## 1 Ali M 2014 Basenji Manhat… 10013 2014-09-12 2019-09-12 2018 ## 2 Ian M 2006 Unknown Manhat… 10013 2014-09-12 2019-10-30 2018 ## 3 Chewba… F 2012 Labrad… Manhat… 10013 2014-09-12 2019-10-01 2018 ## 4 Lola F 2006 Miniat… Manhat… 10022 2014-09-13 2019-10-03 2018 ## 5 Lucy F 2014 Dachsh… Brookl… 11215 2014-09-13 2019-09-13 2018 ## 6 June F 2010 Cavali… Brookl… 11238 2014-09-13 2019-10-28 2018 ## 7 Apple M 2013 Havane… Manhat… 10025 2014-09-13 2019-10-16 2018 ## 8 Muneca F 2013 Beagle Brookl… 11232 2014-09-13 2019-09-13 2018 ## 9 Benson M 2010 Boxer Brookl… 11209 2014-09-13 2019-10-29 2018 ## 10 Bigs M 2004 Pit Bu… Brookl… 11208 2014-09-13 2019-10-27 2018 ## # … with 117,361 more rows, and abbreviated variable names ¹animal_name, ## # ²animal_gender, ³animal_birth_year, ⁴breed_rc, ⁵zip_code, ## # ⁶license_issued_date, ⁷license_expired_date, ⁸extract_year ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> * tally() ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 18,945 × 3 ## # Groups: breed_rc [311] ## breed_rc zip_code n ## <chr> <int> <int> ## 1 Affenpinscher 10005 1 ## 2 Affenpinscher 10011 1 ## 3 Affenpinscher 10013 1 ## 4 Affenpinscher 10014 1 ## 5 Affenpinscher 10016 1 ## 6 Affenpinscher 10017 1 ## 7 Affenpinscher 10018 1 ## 8 Affenpinscher 10019 1 ## 9 Affenpinscher 10021 1 ## 10 Affenpinscher 10023 1 ## # … with 18,935 more rows ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> * ungroup() ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 18,945 × 3 ## breed_rc zip_code n ## <chr> <int> <int> ## 1 Affenpinscher 10005 1 ## 2 Affenpinscher 10011 1 ## 3 Affenpinscher 10013 1 ## 4 Affenpinscher 10014 1 ## 5 Affenpinscher 10016 1 ## 6 Affenpinscher 10017 1 ## 7 Affenpinscher 10018 1 ## 8 Affenpinscher 10019 1 ## 9 Affenpinscher 10021 1 ## 10 Affenpinscher 10023 1 ## # … with 18,935 more rows ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> ungroup() |> * complete(zip_code, breed_rc, * fill = list(n = 0)) ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 137,151 × 3 ## zip_code breed_rc n ## <int> <chr> <int> ## 1 1135 Affenpinscher 0 ## 2 1135 Afghan Hound 0 ## 3 1135 Afghan Hound Crossbreed 0 ## 4 1135 Airedale Terrier 0 ## 5 1135 Akita 0 ## 6 1135 Akita Crossbreed 0 ## 7 1135 Alaskan Malamute 0 ## 8 1135 American Bully 0 ## 9 1135 American English Coonhound 0 ## 10 1135 American Eskimo Dog 0 ## # … with 137,141 more rows ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> ungroup() |> complete(zip_code, breed_rc, fill = list(n = 0)) |> # Regroup to get the right denominator * group_by(breed_rc) ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 137,151 × 3 ## # Groups: breed_rc [311] ## zip_code breed_rc n ## <int> <chr> <int> ## 1 1135 Affenpinscher 0 ## 2 1135 Afghan Hound 0 ## 3 1135 Afghan Hound Crossbreed 0 ## 4 1135 Airedale Terrier 0 ## 5 1135 Akita 0 ## 6 1135 Akita Crossbreed 0 ## 7 1135 Alaskan Malamute 0 ## 8 1135 American Bully 0 ## 9 1135 American English Coonhound 0 ## 10 1135 American Eskimo Dog 0 ## # … with 137,141 more rows ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> ungroup() |> complete(zip_code, breed_rc, fill = list(n = 0)) |> # Regroup to get the right denominator group_by(breed_rc) |> * mutate(freq = n / sum(n)) ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 137,151 × 4 ## # Groups: breed_rc [311] ## zip_code breed_rc n freq ## <int> <chr> <int> <dbl> ## 1 1135 Affenpinscher 0 0 ## 2 1135 Afghan Hound 0 0 ## 3 1135 Afghan Hound Crossbreed 0 0 ## 4 1135 Airedale Terrier 0 0 ## 5 1135 Akita 0 0 ## 6 1135 Akita Crossbreed 0 0 ## 7 1135 Alaskan Malamute 0 0 ## 8 1135 American Bully 0 0 ## 9 1135 American English Coonhound 0 0 ## 10 1135 American Eskimo Dog 0 0 ## # … with 137,141 more rows ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> ungroup() |> complete(zip_code, breed_rc, fill = list(n = 0)) |> # Regroup to get the right denominator group_by(breed_rc) |> mutate(freq = n / sum(n)) |> * filter(breed_rc == "French Bulldog") ``` ] .panel2-reveal-bulldogzeros-auto[ ``` ## # A tibble: 441 × 4 ## # Groups: breed_rc [1] ## zip_code breed_rc n freq ## <int> <chr> <int> <dbl> ## 1 1135 French Bulldog 0 0 ## 2 1175 French Bulldog 0 0 ## 3 1305 French Bulldog 0 0 ## 4 6403 French Bulldog 0 0 ## 5 6473 French Bulldog 0 0 ## 6 6518 French Bulldog 0 0 ## 7 6615 French Bulldog 0 0 ## 8 6901 French Bulldog 0 0 ## 9 7002 French Bulldog 0 0 ## 10 7010 French Bulldog 0 0 ## # … with 431 more rows ``` ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> ungroup() |> complete(zip_code, breed_rc, fill = list(n = 0)) |> # Regroup to get the right denominator group_by(breed_rc) |> mutate(freq = n / sum(n)) |> filter(breed_rc == "French Bulldog") -> * nyc_fb2 ``` ] .panel2-reveal-bulldogzeros-auto[ ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> ungroup() |> complete(zip_code, breed_rc, fill = list(n = 0)) |> # Regroup to get the right denominator group_by(breed_rc) |> mutate(freq = n / sum(n)) |> filter(breed_rc == "French Bulldog") -> nyc_fb2 *fb_map2 <- left_join(nyc_zips, * nyc_fb2, * by = "zip_code") ``` ] .panel2-reveal-bulldogzeros-auto[ ] --- count: false # Keep the Zero count zips .panel1-reveal-bulldogzeros-auto[ ```r nyc_license |> filter(extract_year == 2018) |> group_by(breed_rc, zip_code) |> tally() |> ungroup() |> complete(zip_code, breed_rc, fill = list(n = 0)) |> # Regroup to get the right denominator group_by(breed_rc) |> mutate(freq = n / sum(n)) |> filter(breed_rc == "French Bulldog") -> nyc_fb2 fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") ``` ] .panel2-reveal-bulldogzeros-auto[ ] <style> .panel1-reveal-bulldogzeros-auto { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-reveal-bulldogzeros-auto { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-reveal-bulldogzeros-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Ready to map, again ```r fb_map2 |> select(zip_code, po_name, borough, breed_rc:freq, geometry) ``` ``` ## Simple feature collection with 262 features and 6 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 7 ## zip_code po_name borough breed…¹ n freq geometry ## <int> <chr> <chr> <chr> <int> <dbl> <POLYGON [°]> ## 1 11372 Jackson Hei… Queens French… 13 8.02e-3 ((-73.86942 40.74916, -7… ## 2 11004 Glen Oaks Queens French… 1 6.17e-4 ((-73.71068 40.75004, -7… ## 3 11040 New Hyde Pa… Queens French… 0 0 ((-73.70098 40.7389, -73… ## 4 11426 Bellerose Queens French… 1 6.17e-4 ((-73.7227 40.75373, -73… ## 5 11365 Fresh Meado… Queens French… 7 4.32e-3 ((-73.81089 40.72717, -7… ## 6 11373 Elmhurst Queens French… 14 8.64e-3 ((-73.88722 40.72753, -7… ## 7 11001 Floral Park Queens French… 0 0 ((-73.70098 40.7389, -73… ## 8 11375 Forest Hills Queens French… 8 4.94e-3 ((-73.85625 40.73672, -7… ## 9 11427 Queens Vill… Queens French… 2 1.23e-3 ((-73.74169 40.73682, -7… ## 10 11374 Rego Park Queens French… 6 3.70e-3 ((-73.86451 40.73407, -7… ## # … with 252 more rows, and abbreviated variable name ¹breed_rc ``` - This time, a number of previous .kjh-red[NA] rows are now zeroes instead. --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again *load("fbnyc2.Rdata") ``` ] .panel2-reveal-bulldogfb2-auto[ ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") *#fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") ``` ] .panel2-reveal-bulldogfb2-auto[ ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") *fb_map2 ``` ] .panel2-reveal-bulldogfb2-auto[ ``` ## Simple feature collection with 262 features and 14 fields ## Geometry type: POLYGON ## Dimension: XY ## Bounding box: xmin: -74.25576 ymin: 40.49584 xmax: -73.6996 ymax: 40.91517 ## Geodetic CRS: WGS 84 ## # A tibble: 262 × 15 ## objec…¹ zip_c…² po_name state borough st_fips cty_f…³ bld_g…⁴ shape…⁵ shape…⁶ ## <int> <int> <chr> <chr> <chr> <chr> <chr> <int> <dbl> <dbl> ## 1 1 11372 Jackso… NY Queens 36 081 0 20625. 2.02e7 ## 2 2 11004 Glen O… NY Queens 36 081 0 23003. 2.26e7 ## 3 3 11040 New Hy… NY Queens 36 081 0 15749. 6.27e6 ## 4 4 11426 Beller… NY Queens 36 081 0 35933. 4.94e7 ## 5 5 11365 Fresh … NY Queens 36 081 0 38694. 6.94e7 ## 6 6 11373 Elmhur… NY Queens 36 081 0 33756. 4.27e7 ## 7 7 11001 Floral… NY Queens 36 081 0 13595. 9.16e6 ## 8 8 11375 Forest… NY Queens 36 081 0 36277. 5.56e7 ## 9 9 11427 Queens… NY Queens 36 081 0 31232. 3.96e7 ## 10 10 11374 Rego P… NY Queens 36 081 0 26324. 2.52e7 ## # … with 252 more rows, 5 more variables: x_id <chr>, geometry <POLYGON [°]>, ## # breed_rc <chr>, n <int>, freq <dbl>, and abbreviated variable names ## # ¹objectid, ²zip_code, ³cty_fips, ⁴bld_gpostal_code, ⁵shape_leng, ## # ⁶shape_area ``` ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> * ggplot(mapping = aes(fill = freq)) ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_04_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + * geom_sf(color = "gray30", size = 0.1) ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_05_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + * scale_fill_continuous_sequential( * palette = "Oranges", * labels = scales::label_percent()) ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_06_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + * labs(fill = "Percent of all French Bulldogs") ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_07_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + * annotate(geom = "text", * x = -74.145 + 0.029, * y = 40.82-0.012, * label = "New York City's French Bulldogs", * size = 6) ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_08_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + * annotate(geom = "text", * x = -74.1468 + 0.029, * y = 40.7955, * label = "By Zip Code. Based on Licensing Data", * size = 5) ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_09_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.7955, label = "By Zip Code. Based on Licensing Data", size = 5) + * kjhslides::kjh_theme_nymap() ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_10_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.7955, label = "By Zip Code. Based on Licensing Data", size = 5) + kjhslides::kjh_theme_nymap() + * guides(fill = * guide_legend(title.position = "top", * label.position = "bottom", * keywidth = 1, * nrow = 1)) ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_11_output-1.png" width="504" style="display: block; margin: auto;" /> ] --- count: false # Now redraw the map .panel1-reveal-bulldogfb2-auto[ ```r # Slide workaround again load("fbnyc2.Rdata") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") #fb_map2 <- left_join(nyc_zips, nyc_fb2, by = "zip_code") fb_map2 |> ggplot(mapping = aes(fill = freq)) + geom_sf(color = "gray30", size = 0.1) + scale_fill_continuous_sequential( palette = "Oranges", labels = scales::label_percent()) + labs(fill = "Percent of all French Bulldogs") + annotate(geom = "text", x = -74.145 + 0.029, y = 40.82-0.012, label = "New York City's French Bulldogs", size = 6) + annotate(geom = "text", x = -74.1468 + 0.029, y = 40.7955, label = "By Zip Code. Based on Licensing Data", size = 5) + kjhslides::kjh_theme_nymap() + guides(fill = guide_legend(title.position = "top", label.position = "bottom", keywidth = 1, nrow = 1)) ``` ] .panel2-reveal-bulldogfb2-auto[ <img src="09b-slides_files/figure-html/reveal-bulldogfb2_auto_12_output-1.png" width="504" style="display: block; margin: auto;" /> ] <style> .panel1-reveal-bulldogfb2-auto { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-reveal-bulldogfb2-auto { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-reveal-bulldogfb2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- layout: false <img src="09b-slides_files/figure-html/07-make-maps-61-1.png" width="864" style="display: block; margin: auto;" /> .right.w90.small[Zero areas properly zero, missing areas properly missing.] --- layout: true class: title title-1 --- # Care with Spatial Distribution .center[] --- # Care with Spatial Distribution .center[] --- # Care with Spatial Distribution .center[] --- # Dorling Cartograms ```r # install.packages("cartogram") library(cartogram) options(tigris_use_cache = TRUE) ``` --- # Dorling Cartograms ```r pop_names <- tribble( ~varname, ~clean, "B01003_001", "pop", "B01001B_001", "black", "B01001A_001", "white", "B01001H_001", "nh_white", "B01001I_001", "hispanic", "B01001D_001", "asian" ) pop_names ``` ``` ## # A tibble: 6 × 2 ## varname clean ## <chr> <chr> ## 1 B01003_001 pop ## 2 B01001B_001 black ## 3 B01001A_001 white ## 4 B01001H_001 nh_white ## 5 B01001I_001 hispanic ## 6 B01001D_001 asian ``` --- # Dorling Cartograms ```r clean_names <- function(variable, lookup) { vtbl <- lookup for(i in 1:nrow(vtbl)) { variable <- stringr::str_replace(variable, vtbl$varname[i], vtbl$clean[i]) } variable } ``` --- # Dorling Cartograms ```r fips_pop <- get_acs(geography = "county", variables = pop_names$varname, cache_table = TRUE) |> mutate(variable = clean_names(variable, lookup = pop_names)) |> select(-moe) |> pivot_wider(names_from = variable, values_from = estimate) |> rename(fips = GEOID, name = NAME) |> mutate(prop_pop = pop/sum(pop), prop_black = black/pop, prop_hisp = hispanic/pop, prop_white = white/pop, prop_nhwhite = nh_white/pop, prop_asian = asian/pop) fips_map <- get_acs(geography = "county", variables = "B01001_001", geometry = TRUE, shift_geo = FALSE, cache_table = TRUE) |> select(GEOID, NAME, geometry) |> rename(fips = GEOID, name = NAME) ``` --- # Dorling Cartograms ```r pop_cat_labels <- c("<5", as.character(seq(10, 95, 5)), "100") counties_sf <- fips_map |> left_join(fips_pop, by = c("fips", "name")) |> mutate(black_disc = cut(prop_black*100, breaks = seq(0, 100, 5), labels = pop_cat_labels, ordered_result = TRUE), hisp_disc = cut(prop_hisp*100, breaks = seq(0, 100, 5), labels = pop_cat_labels, ordered_result = TRUE), nhwhite_disc = cut(prop_nhwhite*100, breaks = seq(0, 100, 5), labels = pop_cat_labels, ordered_result = TRUE), asian_disc = cut(prop_asian*100, breaks = seq(0, 100, 5), labels = pop_cat_labels, ordered_result = TRUE)) |> sf::st_transform(crs = 2163) ``` --- # Dorling Cartograms ```r counties_sf ``` ``` ## Simple feature collection with 3221 features and 18 fields ## Geometry type: MULTIPOLYGON ## Dimension: XY ## Bounding box: xmin: -6433624 ymin: -2354609 xmax: 3668029 ymax: 3912355 ## Projected CRS: NAD27 / US National Atlas Equal Area ## First 10 features: ## fips name white black asian nh_white hispanic ## 1 20161 Riley County, Kansas 58797 4509 3414 55420 6125 ## 2 19159 Ringgold County, Iowa 4525 4 9 4494 128 ## 3 30009 Carbon County, Montana 10013 77 44 9773 284 ## 4 16007 Bear Lake County, Idaho 5945 14 18 5839 283 ## 5 55011 Buffalo County, Wisconsin 12789 98 5 12643 335 ## 6 31185 York County, Nebraska 13234 206 32 12780 743 ## 7 08037 Eagle County, Colorado 45327 504 807 36122 16400 ## 8 42129 Westmoreland County, Pennsylvania 332528 8233 3590 330055 4640 ## 9 40079 Le Flore County, Oklahoma 35333 873 386 33833 3560 ## 10 48053 Burnet County, Texas 41346 708 331 35042 10987 ## pop prop_pop prop_black prop_hisp prop_white prop_nhwhite ## 1 72602 2.180000e-04 0.0621057271 0.08436407 0.8098537 0.7633399 ## 2 4739 1.422966e-05 0.0008440599 0.02700992 0.9548428 0.9483013 ## 3 10488 3.149202e-05 0.0073417239 0.02707857 0.9547101 0.9318268 ## 4 6327 1.899790e-05 0.0022127391 0.04472894 0.9396238 0.9228702 ## 5 13314 3.997757e-05 0.0073606730 0.02516148 0.9605678 0.9496019 ## 6 14164 4.252984e-05 0.0145439141 0.05245693 0.9343406 0.9022875 ## 7 55693 1.672278e-04 0.0090496113 0.29447148 0.8138725 0.6485914 ## 8 355107 1.066270e-03 0.0231845613 0.01306648 0.9364163 0.9294522 ## 9 48436 1.454374e-04 0.0180237840 0.07349905 0.7294781 0.6985094 ## 10 48424 1.454014e-04 0.0146208492 0.22689162 0.8538328 0.7236494 ## prop_asian geometry black_disc hisp_disc ## 1 0.0470234980 MULTIPOLYGON (((262454.9 -6... 10 10 ## 2 0.0018991348 MULTIPOLYGON (((466316.5 -4... <5 <5 ## 3 0.0041952708 MULTIPOLYGON (((-767960.6 6... <5 <5 ## 4 0.0028449502 MULTIPOLYGON (((-951410.2 -... <5 <5 ## 5 0.0003755445 MULTIPOLYGON (((629304.5 -3... <5 <5 ## 6 0.0022592488 MULTIPOLYGON (((182859.7 -4... <5 10 ## 7 0.0144901514 MULTIPOLYGON (((-610949.3 -... <5 30 ## 8 0.0101096289 MULTIPOLYGON (((1692662 -33... <5 <5 ## 9 0.0079692790 MULTIPOLYGON (((453755.9 -1... <5 10 ## 10 0.0068354535 MULTIPOLYGON (((148220 -154... <5 25 ## nhwhite_disc asian_disc ## 1 80 <5 ## 2 95 <5 ## 3 95 <5 ## 4 95 <5 ## 5 95 <5 ## 6 95 <5 ## 7 65 <5 ## 8 95 <5 ## 9 70 <5 ## 10 75 <5 ``` --- # Dorling Cartograms ```r ## Be patient county_dorling <- cartogram_dorling(x = counties_sf, weight = "prop_pop", k = 0.2, itermax = 100) out_black <- county_dorling |> filter(!str_detect(name, "Alaska|Hawaii|Puerto|Guam")) |> ggplot(aes(fill = black_disc)) + geom_sf(color = "grey30", size = 0.1) + coord_sf(crs = 2163, datum = NA) + scale_fill_discrete_sequential(palette = "YlOrBr", na.translate=FALSE) + guides(fill = guide_legend(title.position = "top", label.position = "bottom", nrow = 1)) + labs( subtitle = "Bubble size corresponds to County Population", caption = "Graph: @kjhealy. Source: Census Bureau / American Community Survey", fill = "Percent Black by County") + theme(legend.position = "top", legend.spacing.x = unit(0, "cm"), legend.title = element_text(size = rel(1.5), face = "bold"), legend.text = element_text(size = rel(0.7)), plot.title = element_text(size = rel(1.4), hjust = 0.15)) # ggsave("figures/dorling-bl.pdf", out_black, height = 10, width = 12) ``` --- # Dorling Cartograms --- layout: false ```r print(out_black) ``` <img src="09b-slides_files/figure-html/unnamed-chunk-10-1.png" width="720" style="display: block; margin: auto;" /> --- ```r print(out_white) ``` <img src="09b-slides_files/figure-html/unnamed-chunk-11-1.png" width="720" style="display: block; margin: auto;" /> --- ```r print(out_hispanic) ``` <img src="09b-slides_files/figure-html/unnamed-chunk-12-1.png" width="720" style="display: block; margin: auto;" /> --- ```r print(out_asian) ``` <img src="09b-slides_files/figure-html/unnamed-chunk-13-1.png" width="720" style="display: block; margin: auto;" />