How to map tidycensus list output?


#1

I am using the tidycensus() package to obtain Census data on Illinois. I put geometry = TRUE. However, the geometry output is not a shapefile but a list of list of a vector. How do I either 1) map that in leaflet() or 2) convert it to something I can map in leaflet()? Thanks!

library(tidycensus)
library(tidyverse)
library(leaflet)

il <-
  get_decennial(
    geography = "block",
    variables = "P005002",
    year = 2010,
    state = "IL",
    county = "Cook",
    geometry = TRUE
  )
#> Getting data from the 2010 decennial Census
#> Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

leaflet(il) %>%
  addProviderTiles(provider = "CartoDB") %>%
  addPolygons()
#> Warning message:
sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84' 


#2

There is a great blogpost on how to use tidycensus with leaflet
https://juliasilge.com/blog/using-tidycensus/

Could apply to your use case. You may need some sf transformation.


#3

You won't get a shapefile @shortessay - you will get an sf object instead:

> library(tidycensus)
> library(leaflet)
>
> ilPop <- get_acs(geography = "county", variables = "B01003_001", state = "IL", geometry = TRUE)  
Getting data from the 2012-2016 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
Using FIPS code '17' for state 'IL'
> class(ilPop)
[1] "sf"         "data.frame"

sf objects are a way of representing the data you would store in a shapefile. You can use the sf package to read and write shapefiles, and also manipulate their characteristics. The ilPop$geometry column contains all of the spatial data you need for plotting.

> leaflet(ilPop) %>%
+     addProviderTiles(provider = "CartoDB") %>%
+     addPolygons()
Warning message:
sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
Need '+proj=longlat +datum=WGS84' 

It should map even with the "inconsistent datum" since the data are in latitude-longitude (NAD 1983). If they were in a projected coordinate system, they would not map. If you are using RStudio, the map should appear in your Viewer tab.

FWIW, I tried running your get_decennial() call and got an error. Can you be a bit more specific about what does or does not happen when you run your leaflet pipeline?


#4

The warning can be removed by doing what it recommends, and changing to a CRS in long/lat with a WGS84 datum, e.g. with sf::st_transform(4326). Using @chris.prener's example,

library(leaflet)

ilPop <- tidycensus::get_acs(
    geography = "county", 
    variables = "B01003_001", 
    state = "IL", 
    geometry = TRUE, 
    key = keyring::key_get('census')
)

ilPop %>% 
    sf::st_transform(4326) %>% 
    leaflet() %>% 
    addProviderTiles("CartoDB") %>% 
    addPolygons(
        # logged color scale because otherwise every county besides Cook is the same
        fillColor = ~colorNumeric("viridis", log(estimate))(log(estimate)), 
        stroke = FALSE
    )

leaflet screenshot with IL county polygons

For more on sf, see the vignettes, which are pretty solid. For more on projections and geospatial data generally, see the excellent Geocomputation with R: