── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Code
library(tidycensus) # Tidily talk to the Censuslibrary(sf) # Draw maps with ggplot
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
# A tibble: 8,959 × 3
name label concept
<chr> <chr> <chr>
1 H001001 Total HOUSING UNITS
2 H002001 Total URBAN AND RURAL
3 H002002 Total!!Urban URBAN AND RURAL
4 H002003 Total!!Urban!!Inside urbanized areas URBAN AND RURAL
5 H002004 Total!!Urban!!Inside urban clusters URBAN AND RURAL
6 H002005 Total!!Rural URBAN AND RURAL
7 H002006 Total!!Not defined for this file URBAN AND RURAL
8 H003001 Total OCCUPANCY STATUS
9 H003002 Total!!Occupied OCCUPANCY STATUS
10 H003003 Total!!Vacant OCCUPANCY STATUS
# ℹ 8,949 more rows
Some Core Population Measures by County for North Carolina
Code
# Census variable namespopvars <-c("P005003", "P005004", "P005006", "P004003")# Get a county-level dataset for NC with these variablesnc <-get_decennial(geography ="county",variables = popvars,year =2010,summary_var ="P001001",state ="NC") |>mutate(pct =100* (value / summary_value))
Getting data from the 2010 decennial Census
Using Census Summary File 1
Code
nc
# A tibble: 400 × 6
GEOID NAME variable value summary_value pct
<chr> <chr> <chr> <dbl> <dbl> <dbl>
1 37007 Anson County, North Carolina P005003 12344 26948 45.8
2 37011 Avery County, North Carolina P005003 16029 17797 90.1
3 37003 Alexander County, North Carolina P005003 32671 37198 87.8
4 37015 Bertie County, North Carolina P005003 7393 21282 34.7
5 37013 Beaufort County, North Carolina P005003 31705 47759 66.4
6 37005 Alleghany County, North Carolina P005003 9862 11155 88.4
7 37001 Alamance County, North Carolina P005003 101718 151131 67.3
8 37009 Ashe County, North Carolina P005003 25420 27281 93.2
9 37017 Bladen County, North Carolina P005003 19242 35190 54.7
10 37019 Brunswick County, North Carolina P005003 86818 107431 80.8
# ℹ 390 more rows
ACS Data
Code
# Get median HH income by county for NCnc_inc <-get_acs(geography ="county",variables =c(medincome ="B19013_001"),state ="NC",year =2020)
Getting data from the 2016-2020 5-year ACS
Code
nc_inc
# A tibble: 100 × 5
GEOID NAME variable estimate moe
<chr> <chr> <chr> <dbl> <dbl>
1 37001 Alamance County, North Carolina medincome 51580 1749
2 37003 Alexander County, North Carolina medincome 51329 3341
3 37005 Alleghany County, North Carolina medincome 37158 5049
4 37007 Anson County, North Carolina medincome 39799 5189
5 37009 Ashe County, North Carolina medincome 43030 2327
6 37011 Avery County, North Carolina medincome 42695 3551
7 37013 Beaufort County, North Carolina medincome 48051 3675
8 37015 Bertie County, North Carolina medincome 35042 3090
9 37017 Bladen County, North Carolina medincome 37188 4231
10 37019 Brunswick County, North Carolina medincome 59763 1672
# ℹ 90 more rows
Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
give correct results for longitude/latitude data
Source Code
---title: "Example 07: Social Data and the State"---## Setup```{r}library(here) # manage file pathslibrary(socviz) # data and some useful functionslibrary(tidyverse) # your friend and minelibrary(tidycensus) # Tidily talk to the Censuslibrary(sf) # Draw maps with ggplot# Don't needlessly download geo files multiple timesoptions(tigris_use_cache =TRUE)```## Make sure you have a Census API keyAs we discussed in class, you'll need an API key to talk to the Census. Get one here:<https://api.census.gov/data/key_signup.html>Follow the instructions and confirm the key via email when you get it. Keep the email. Load your key with the `census_api_key()` function. Paste your key (in quotes) instead of "YOUR API KEY GOES HERE" in the chunk below.```{r}# census_api_key("YOUR API KEY GOES HERE")```## Median Age by State```{r}age10 <-get_decennial(geography ="state",variables ="P013001",year =2010)age10```## Decennial Census VariablesThere are a lot of them. ```{r}census_vars <-load_variables(year =2010,dataset ="sf1",cache =TRUE)census_vars```## Some Core Population Measures by County for North Carolina```{r}# Census variable namespopvars <-c("P005003", "P005004", "P005006", "P004003")# Get a county-level dataset for NC with these variablesnc <-get_decennial(geography ="county",variables = popvars,year =2010,summary_var ="P001001",state ="NC") |>mutate(pct =100* (value / summary_value))nc```## ACS Data```{r}# Get median HH income by county for NCnc_inc <-get_acs(geography ="county",variables =c(medincome ="B19013_001"),state ="NC",year =2020)nc_inc```With geographical information:```{r, message=FALSE, results='hide'}nc_inc <- get_acs(geography = "county", variables = c(medincome = "B19013_001"), state = "NC", year = 2020, geometry = TRUE)``````{r}# Now we have a GEOMETRY column and spatial features informationnc_inc```## Draw a Map```{r, fig.height=5, fig.width=9}nc_inc |> mutate(slab = stringr::str_remove(NAME, " County.*"), estimate = estimate/1000) |> ggplot(mapping = aes(fill = estimate)) + geom_sf(color = "white") + geom_sf_text(aes(label = slab), color = "white", size = rel(0.9)) + scale_fill_continuous(labels = scales::label_dollar()) + guides(fill = guide_legend(keywidth = rel(2.2), title.position = "top", label.position = "bottom")) + labs(fill = "Median HH Income ('000s)") + theme(legend.position = "bottom")```