understanding census geography and tigris basics
play

Understanding Census geography and tigris basics Kyle Walker - PowerPoint PPT Presentation

DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Understanding Census geography and tigris basics Kyle Walker Instructor DataCamp Analyzing US Census Data in R TIGER/Line Shapefiles DataCamp Analyzing US Census Data in


  1. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Understanding Census geography and tigris basics Kyle Walker Instructor

  2. DataCamp Analyzing US Census Data in R TIGER/Line Shapefiles

  3. DataCamp Analyzing US Census Data in R The tigris R package Functions in the tigris package: Download shapefiles from the Census website Load them into R as spatial objects

  4. DataCamp Analyzing US Census Data in R Legal and statistical entities library(tigris) az_counties <- counties(state = "AZ") plot(az_counties)

  5. DataCamp Analyzing US Census Data in R Geographic features nh_roads <- primary_secondary_roads(state = "NH") plot(nh_roads)

  6. DataCamp Analyzing US Census Data in R tigris and Spatial objects head(nh_roads@data) LINEARID FULLNAME RTTYP MTFCC 0 110450358068 Hanover St Exd M S1200 1 110426481978 State Rte 28 Byp S S1200 2 110426481286 US Hwy 1 Byp U S1200 3 110426481285 US Hwy 1 Byp U S1200 4 110408217438 State Rte 28 Byp S S1200 5 110426481980 State Rte 28 Byp S S1200 nh_roads@proj4string CRS arguments: +proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0 More on coordinate reference systems from Geocomputation with R

  7. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Let's practice!

  8. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Customizing tigris options Kyle Walker Instructor

  9. DataCamp Analyzing US Census Data in R Cartographic boundary shapefiles TIGER/Line shapefiles include water area of entities Can be unsuitable for thematic mapping Alternative: cartographic boundary shapefiles Available in tigris with argument cb = TRUE

  10. DataCamp Analyzing US Census Data in R TIGER/Line vs. cartographic boundary files ri_tiger <- counties("RI") ri_cb <- counties("RI", cb = TRUE) par(mfrow = c(1, 2)) plot(ri_tiger, main = "TIGER/Line") plot(ri_cb, main = "Cartographic boundary")

  11. DataCamp Analyzing US Census Data in R tigris and simple features options(tigris_class = "sf") az_sf <- counties("AZ", cb = TRUE) class(az_sf) [1] "sf" "data.frame" st_geometry(az_sf) Geometry set for 15 features geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -114.8165 ymin: 31.33218 xmax: -109.0452 ymax: 37.00426 epsg (SRID): 4269 proj4string: +proj=longlat +datum=NAD83 +no_defs

  12. DataCamp Analyzing US Census Data in R Caching tigris data Waiting for large Census shapefiles to download can be tedious! Alternative: options(tigris_use_cache = TRUE) To set the cache directory: tigris_cache_dir() function

  13. DataCamp Analyzing US Census Data in R Historical data and tigris Historical data can be obtained in tigris by using the year parameter Data available for 1990, 2000, 2010, and 2011-2017 williamson90 <- tracts(state = "TX", county = "Williamson", cb = TRUE, year = 1990) williamson16 <- tracts(state = "TX", county = "Williamson", cb = TRUE, year = 2016) par(mfrow = c(1, 2)) plot(williamson90$geometry) plot(williamson16$geometry)

  14. DataCamp Analyzing US Census Data in R

  15. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Let's practice!

  16. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Combining and joining Census geographic datasets Kyle Walker Instructor

  17. DataCamp Analyzing US Census Data in R The "tigris" attribute library(tigris) missouri <- tracts("MO", cb = TRUE) kansas <- tracts("KS", cb = TRUE) attr(missouri, "tigris") [1] "tract" attr(kansas, "tigris") [1] "tract"

  18. DataCamp Analyzing US Census Data in R Combining datasets with rbind_tigris() kansas_missouri <- rbind_tigris(kansas, missouri) plot(kansas_missouri$geometry)

  19. DataCamp Analyzing US Census Data in R Combining datasets with tidyverse tools library(tidyverse) new_england <- c("ME", "NH", "VT", "MA") ne_tracts <- map(new_england, function(x) { tracts(state = x, cb = TRUE) }) %>% rbind_tigris()

  20. DataCamp Analyzing US Census Data in R Joining data from a data frame library(tidyverse) library(sf) tx_house <- state_legislative_districts(state = "TX", house = "lower", cb = TRUE) tx_joined <- left_join(tx_house, tx_members, by = c("NAME" = "District"))

  21. DataCamp Analyzing US Census Data in R Joining data from a data frame glimpse(tx_joined) Observations: 150 Variables: 13 $ STATEFP <chr> "48", "48", "48", "48", "48", "48", "48", ... $ SLDLST <chr> "030", "060", "007", "109", "073", "028", ... $ AFFGEOID <chr> "620L500US48030", "620L500US48060", "620L5... $ GEOID <chr> "48030", "48060", "48007", "48109", "48073... $ NAME <chr> "30", "60", "7", "109", "73", "28", "10", ... $ LSAD <chr> "LL", "LL", "LL", "LL", "LL", "LL", "LL", ... $ LSY <chr> "2016", "2016", "2016", "2016", "2016", "2... $ ALAND <dbl> 10806729636, 18689750348, 2217822139, 4498... $ AWATER <dbl> 2240468800, 297318151, 31225452, 10302637,... $ Name <chr> "Morrison, Geanie W.", "Lang, Mike", "Dean... $ City <chr> "Victoria", "Granbury", "Longview", "DeSot... $ Party <chr> "R", "R", "R", "D", "R", "R", "R", "D", "D... $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-97.07866 2.....

  22. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Let's practice!

  23. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Plotting data with tigris and ggplot2 Kyle Walker Instructor

  24. DataCamp Analyzing US Census Data in R Plotting spatial data with geom_sf() ggplot(tx_joined) + geom_sf()

  25. DataCamp Analyzing US Census Data in R Setting the fill aesthetic for regions ggplot(tx_joined, aes(fill = Party)) + geom_sf()

  26. DataCamp Analyzing US Census Data in R Customizing fill colors ggplot(tx_joined, aes(fill = Party)) + geom_sf() + scale_fill_manual(values = c("R" = "red", "D" = "blue"))

  27. DataCamp Analyzing US Census Data in R Polishing up the output ggplot(tx_joined, aes(fill = Party)) + geom_sf() + coord_sf(crs = 3083, datum = NA) + scale_fill_manual(values = c("R" = "red", "D" = "blue")) + theme_minimal() + labs(title = "State House Districts in Texas")

  28. DataCamp Analyzing US Census Data in R

  29. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Let's practice!

Recommend


More recommend