simple feature geometry and tidycensus
play

Simple feature geometry and tidycensus Kyle Walker Instructor - PowerPoint PPT Presentation

DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Simple feature geometry and tidycensus Kyle Walker Instructor DataCamp Analyzing US Census Data in R The geometry = TRUE argument geometry = TRUE is available for the


  1. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Simple feature geometry and tidycensus Kyle Walker Instructor

  2. DataCamp Analyzing US Census Data in R The geometry = TRUE argument geometry = TRUE is available for the following geographies: "state" "county" "tract" "block group" "block" "zcta" (also "zip code tabulation area" )

  3. DataCamp Analyzing US Census Data in R Getting simple feature geometry library(tidycensus) library(tidyverse) library(sf) cook_value <- get_acs(geography = "tract", state = "IL", county = "Cook", variables = "B25077_001", geometry = TRUE)

  4. DataCamp Analyzing US Census Data in R Simple feature geometry head(cook_value, 3) Simple feature collection with 3 features and 5 fields geometry type: MULTIPOLYGON dimension: XY bbox: xmin: -87.68465 ymin: 42.01232 xmax: -87.66434 ymax: 42.02297 epsg (SRID): 4269 proj4string: +proj=longlat +datum=NAD83 +no_defs GEOID NAME variable 1 17031010100 Census Tract 101, Cook County, Illinois B25077_001 2 17031010201 Census Tract 102.01, Cook County, Illinois B25077_001 3 17031010202 Census Tract 102.02, Cook County, Illinois B25077_001 estimate moe geometry 1 230700 62332 MULTIPOLYGON (((-87.6772 42... 2 151100 19099 MULTIPOLYGON (((-87.68465 4... 3 133300 84063 MULTIPOLYGON (((-87.67685 4...

  5. DataCamp Analyzing US Census Data in R Plotting tidycensus geometry plot(cook_value["estimate"])

  6. DataCamp Analyzing US Census Data in R Joining tigris and tidycensus data library(tigris) idaho_income <- get_acs(geography = "school district (unified)", variables = "B19013_001", state = "ID") idaho_school <- school_districts(state = "ID", type = "unified", class = "sf") id_school_joined <- left_join(idaho_school, idaho_income, by = "GEOID")

  7. DataCamp Analyzing US Census Data in R Joining tigris and tidycensus data plot(id_school_joined["estimate"])

  8. DataCamp Analyzing US Census Data in R Shifting Alaska and Hawaii geometry state_value <- get_acs(geography = "state", variables = "B25077_001", survey = "acs1", geometry = TRUE, shift_geo = TRUE) plot(state_value["estimate"])

  9. DataCamp Analyzing US Census Data in R

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

  11. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Mapping demographic data with ggplot2 Kyle Walker Instructor

  12. DataCamp Analyzing US Census Data in R A basic choropleth map with geom_sf() library(ggplot2) ggplot(cook_value, aes(fill = estimate)) + geom_sf()

  13. DataCamp Analyzing US Census Data in R

  14. DataCamp Analyzing US Census Data in R Modifying map colors ggplot(cook_value, aes(fill = estimate, color = estimate)) + geom_sf() + scale_fill_viridis_c() + scale_color_viridis_c()

  15. DataCamp Analyzing US Census Data in R Customizing the map output ggplot(cook_value, aes(fill = estimate, color = estimate)) + geom_sf() + scale_fill_viridis_c(labels = scales::dollar) + scale_color_viridis_c(guide = FALSE) + theme_minimal() + coord_sf(crs = 26916, datum = NA) + labs(title = "Median home value by Census tract", subtitle = "Cook County, Illinois", caption = "Data source: 2012-2016 ACS.\nData acquired with the R tidycensus package.", fill = "ACS estimate")

  16. DataCamp Analyzing US Census Data in R

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

  18. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Advanced demographic mapping Kyle Walker Instructor

  19. DataCamp Analyzing US Census Data in R Visual variables in cartography Visual variables by Axis Maps Source: Axis Maps

  20. DataCamp Analyzing US Census Data in R Graduated symbol maps library(sf) centers <- st_centroid(state_value) ggplot() + geom_sf(data = state_value, fill = "white") + geom_sf(data = centers, aes(size = estimate), shape = 21, fill = "lightblue", alpha = 0.7, show.legend = "point") + scale_size_continuous(range = c(1, 20))

  21. DataCamp Analyzing US Census Data in R

  22. DataCamp Analyzing US Census Data in R

  23. DataCamp Analyzing US Census Data in R Faceted maps with ggplot2 ggplot(dc_race, aes(fill = percent, color = percent)) + geom_sf() + coord_sf(datum = NA) + facet_wrap(~variable)

  24. DataCamp Analyzing US Census Data in R Interactive visualization Web-based graphics allow interaction with data Options in R: leaflet plotly htmlwidgets

  25. DataCamp Analyzing US Census Data in R Interactive maps with mapview library(mapview) mapview(cook_value, zcol = "estimate", legend = TRUE)

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

  27. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Cartographic workflows with tigris and tidycensus Kyle Walker Instructor

  28. DataCamp Analyzing US Census Data in R

  29. DataCamp Analyzing US Census Data in R Generating random dots with sf Key function for random point generation in sf : st_sample() dc_dots <- map(c("White", "Black", "Hispanic", "Asian"), function(group) { dc_race %>% filter(variable == group) %>% st_sample(., size = .$value / 100) %>% st_sf() %>% mutate(group = group) }) %>% reduce(rbind)

  30. DataCamp Analyzing US Census Data in R Considerations for random dot generation For faster plotting: dc_dots <- dc_dots %>% group_by(group) %>% summarize() For more accurate visualizations: dc_dots_shuffle <- sample_frac(dc_dots, size = 1)

  31. DataCamp Analyzing US Census Data in R Basic dot-density mapping with sf plot(dc_dots_shuffle, key.pos = 1)

  32. DataCamp Analyzing US Census Data in R Ancillary data with tigris options(tigris_class = "sf") dc_roads <- roads("DC", "District of Columbia") %>% filter(RTTYP %in% c("I", "S", "U")) dc_water <- area_water("DC", "District of Columbia") dc_boundary <- counties("DC", cb = TRUE)

  33. DataCamp Analyzing US Census Data in R Ancillary data with tigris plot(dc_water$geometry, col = "lightblue")

  34. DataCamp Analyzing US Census Data in R Dot-density mapping with ggplot2 ggplot() + geom_sf(data = dc_boundary, color = NA, fill = "white") + geom_sf(data = dc_dots, aes(color = group, fill = group), size = 0.1) + geom_sf(data = dc_water, color = "lightblue", fill = "lightblue") + geom_sf(data = dc_roads, color = "grey") + coord_sf(crs = 26918, datum = NA) + scale_color_brewer(palette = "Set1", guide = FALSE) + scale_fill_brewer(palette = "Set1") + labs(title = "The racial geography of Washington, DC", subtitle = "2010 decennial U.S. Census", fill = "", caption = "1 dot = approximately 100 people.\nData acquired with the R tidycensus and tigris packages.")

  35. DataCamp Analyzing US Census Data in R

  36. DataCamp Analyzing US Census Data in R Considerations for dot-density mapping Be mindful of ways dot-density maps can be misinterpreted Choose qualitative colors wisely Take care when selecting ancillary layers

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

  38. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Other resources for demographic data in R Kyle Walker Instructor

  39. DataCamp Analyzing US Census Data in R Other R packages to know about censusapi ipumsr cancensus

  40. DataCamp Analyzing US Census Data in R Other DataCamp courses Working with Data in the Tidyverse Data Visualization with ggplot2 Interactive Maps with Leaflet in R

  41. DataCamp Analyzing US Census Data in R ANALYZING US CENSUS DATA IN R Thank you!

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend