0

I'm trying to upload data for "county subdivision" as part of the geography options in tidycensus' get_acs() function. I know there are several geography options, which Kyle Walker has published on his page. https://walkerke.github.io/tidycensus/articles/basic-usage.html#geography-in-tidycensus

And while it works fine for state and county level, because you would just put county = "Monmouth". But I can't seem to get the syntax to work at the city subdivision level for a city within Monmouth county. I've looked for other tidycensus scripts, but haven't found any using geographies below County level.

Any suggestions?

library(tidycensus)
library(tidyverse)
library(sf)

census_api_key("YOUR API KEY GOES HERE")

vars <- c(English = "C16002_002", 
      Spanish = "C16002_003")


language <- get_acs(geography = "county subdivision", 
                state = "NJ",
                county = "Monmouth",
                city = "Red Bank",
                table = "C16001")



rb_language <- get_acs(geography = "tract", 
                   variables = vars,
                   state = "NJ", 
                   county = "Monmouth", 
                   city = "Red Bank"
                   geometry = TRUE, 
                   summary_var = "C16002_001") %>%
  st_transform(26918)
bjk127
  • 51
  • 5

1 Answers1

0

I'm not entirely clear if you are trying to get data for the Red Bank county subdivision or census tracts within Red Bank. In either case, you can't do this directly in tidycensus, but rather you can get all subdivisions or tracts in a county using get_acs() and then further filter the results.

For example, if you just want language data for Red Bank county subdivision, you could do this:

library(tidycensus)
library(tidyverse)
library(sf)
library(tigris)

vars <- c(English = "C16002_002", 
          Spanish = "C16002_003")

# get all subdivisions in monmouth county
language_subdiv <- get_acs(geography = "county subdivision", 
                           state = "NJ",
                           county = "Monmouth",
                           table = "C16001")

# only red bank borough
language_subdiv %>% 
  filter(str_detect(NAME, "Red Bank"))
#> # A tibble: 38 x 5
#>    GEOID     NAME                                  variable  estimate   moe
#>    <chr>     <chr>                                 <chr>        <dbl> <dbl>
#>  1 34025624… Red Bank borough, Monmouth County, N… C16001_0…    11405   171
#>  2 34025624… Red Bank borough, Monmouth County, N… C16001_0…     7227   451
#>  3 34025624… Red Bank borough, Monmouth County, N… C16001_0…     3789   425
#>  4 34025624… Red Bank borough, Monmouth County, N… C16001_0…     1287   247
#>  5 34025624… Red Bank borough, Monmouth County, N… C16001_0…     2502   435
#>  6 34025624… Red Bank borough, Monmouth County, N… C16001_0…        0    19
#>  7 34025624… Red Bank borough, Monmouth County, N… C16001_0…        0    19
#>  8 34025624… Red Bank borough, Monmouth County, N… C16001_0…        0    19
#>  9 34025624… Red Bank borough, Monmouth County, N… C16001_0…       42    40
#> 10 34025624… Red Bank borough, Monmouth County, N… C16001_0…        0    19
#> # ... with 28 more rows

Now, if you wanted census tracts within Red Bank, you could grab all census tracts in Monmouth, then use tigris::places() to get the boundaries of Red Bank, and finally filter the census tracts to only get those tracts that are contained by the Red Bank boundary.

# get all tracts in monmouth county
language_tract <- get_acs(geography = "tract", 
                          variables = vars,
                          state = "NJ", 
                          county = "Monmouth",
                          geometry = TRUE, 
                          summary_var = "C16002_001", 
                          output = "wide")

# get geometry of red bank borough 
red_bank_place <- places("NJ", cb = TRUE, class = "sf") %>% 
  filter(NAME == "Red Bank")

# only tracts in red bank borough
red_bank_tracts <- language_tract %>% 
  filter(st_contains(red_bank_place, ., sparse = FALSE))

ggplot() +
  geom_sf(data = red_bank_tracts, color = "blue", fill = NA) +
  geom_sf(data = red_bank_place, color = "black", fill = NA)

Created on 2018-12-24 by the reprex package (v0.2.1)

mfherman
  • 392
  • 4
  • 16
  • Amazing! Thanks. This is the exact geography I was getting at. I'll need to look at how to fill the ggplot because in my original code, I was able to look at the proportion of English and Spanish-speakers. Looked pretty cool and answers my research questions very nicely :) – bjk127 Dec 26 '18 at 22:58