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

simple feature geometry and tidycensus
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

DataCamp Analyzing US Census Data in R

Simple feature geometry and tidycensus

ANALYZING US CENSUS DATA IN R

Kyle Walker

Instructor

slide-2
SLIDE 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")

slide-3
SLIDE 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)

slide-4
SLIDE 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...

slide-5
SLIDE 5

DataCamp Analyzing US Census Data in R

Plotting tidycensus geometry

plot(cook_value["estimate"])

slide-6
SLIDE 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")

slide-7
SLIDE 7

DataCamp Analyzing US Census Data in R

Joining tigris and tidycensus data

plot(id_school_joined["estimate"])

slide-8
SLIDE 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"])

slide-9
SLIDE 9

DataCamp Analyzing US Census Data in R

slide-10
SLIDE 10

DataCamp Analyzing US Census Data in R

Let's practice!

ANALYZING US CENSUS DATA IN R

slide-11
SLIDE 11

DataCamp Analyzing US Census Data in R

Mapping demographic data with ggplot2

ANALYZING US CENSUS DATA IN R

Kyle Walker

Instructor

slide-12
SLIDE 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()

slide-13
SLIDE 13

DataCamp Analyzing US Census Data in R

slide-14
SLIDE 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()

slide-15
SLIDE 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")

slide-16
SLIDE 16

DataCamp Analyzing US Census Data in R

slide-17
SLIDE 17

DataCamp Analyzing US Census Data in R

Let's practice!

ANALYZING US CENSUS DATA IN R

slide-18
SLIDE 18

DataCamp Analyzing US Census Data in R

Advanced demographic mapping

ANALYZING US CENSUS DATA IN R

Kyle Walker

Instructor

slide-19
SLIDE 19

DataCamp Analyzing US Census Data in R

Visual variables in cartography

Visual variables by Axis Maps Source: Axis Maps

slide-20
SLIDE 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))

slide-21
SLIDE 21

DataCamp Analyzing US Census Data in R

slide-22
SLIDE 22

DataCamp Analyzing US Census Data in R

slide-23
SLIDE 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)

slide-24
SLIDE 24

DataCamp Analyzing US Census Data in R

Interactive visualization

Web-based graphics allow interaction with data Options in R:

leaflet plotly htmlwidgets

slide-25
SLIDE 25

DataCamp Analyzing US Census Data in R

Interactive maps with mapview

library(mapview) mapview(cook_value, zcol = "estimate", legend = TRUE)

slide-26
SLIDE 26

DataCamp Analyzing US Census Data in R

Let's practice!

ANALYZING US CENSUS DATA IN R

slide-27
SLIDE 27

DataCamp Analyzing US Census Data in R

Cartographic workflows with tigris and tidycensus

ANALYZING US CENSUS DATA IN R

Kyle Walker

Instructor

slide-28
SLIDE 28

DataCamp Analyzing US Census Data in R

slide-29
SLIDE 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)

slide-30
SLIDE 30

DataCamp Analyzing US Census Data in R

Considerations for random dot generation

For faster plotting: For more accurate visualizations:

dc_dots <- dc_dots %>% group_by(group) %>% summarize() dc_dots_shuffle <- sample_frac(dc_dots, size = 1)

slide-31
SLIDE 31

DataCamp Analyzing US Census Data in R

Basic dot-density mapping with sf

plot(dc_dots_shuffle, key.pos = 1)

slide-32
SLIDE 32

DataCamp Analyzing US Census Data in R

Ancillary data with tigris

  • ptions(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)

slide-33
SLIDE 33

DataCamp Analyzing US Census Data in R

Ancillary data with tigris

plot(dc_water$geometry, col = "lightblue")

slide-34
SLIDE 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.")

slide-35
SLIDE 35

DataCamp Analyzing US Census Data in R

slide-36
SLIDE 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

slide-37
SLIDE 37

DataCamp Analyzing US Census Data in R

Let's practice!

ANALYZING US CENSUS DATA IN R

slide-38
SLIDE 38

DataCamp Analyzing US Census Data in R

Other resources for demographic data in R

ANALYZING US CENSUS DATA IN R

Kyle Walker

Instructor

slide-39
SLIDE 39

DataCamp Analyzing US Census Data in R

Other R packages to know about

censusapi ipumsr cancensus

slide-40
SLIDE 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

slide-41
SLIDE 41

DataCamp Analyzing US Census Data in R

Thank you!

ANALYZING US CENSUS DATA IN R