Reputation: 1
I have a script that pulls GeoIDs utilizing the tidycensus package. This data is very valuable, but I'd also like to pull in latitude and longitudes (even if they are approximate). Problem is, I can't figure out how or if it's even possible. Or if there's any way to join such data utilizing another spatial package. Please see below for code example.
library(tidycensus)
census_api_key("yourkeyhere", install = TRUE)
dfBlocks <- get_decennial(geography = "block", state = "SC", variables = c(tract = "TRACT", state = "STATE"), output = "wide", county = c(c("York County", "Richland County")))
Thanks!
Upvotes: 0
Views: 1311
Reputation: 432
If you specify geometry = TRUE
in your get_decennial()
call, tidycensus
will return polygon geometries along with the census estimates. Then, you can use sf::st_centroid()
to get the centroid (middle) of each polygon.
Here is an example where I get census data and geometry, then create a second sf
object with the centroids of each county polygon. Finally, a quick map so you can see the polygons with their centroids.
library(tidyverse)
library(tidycensus)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
county <- get_decennial(
geography = "county",
state = "SC",
variables = c(county = "COUNTY", state = "STATE"),
output = "wide",
geometry = TRUE
)
#> Getting data from the 2010 decennial Census
count_centroid <- county %>%
st_transform(2273) %>% # convert to projected coord system for better centroid
st_centroid()
#> Warning in st_centroid.sf(.): st_centroid assumes attributes are constant
#> over geometries of x
head(count_centroid)
#> Simple feature collection with 6 features and 4 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: 1506653 ymin: 420377.9 xmax: 1983425 ymax: 1170306
#> epsg (SRID): 2273
#> proj4string: +proj=lcc +lat_1=34.83333333333334 +lat_2=32.5 +lat_0=31.83333333333333 +lon_0=-81 +x_0=609600 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=ft +no_defs
#> # A tibble: 6 x 5
#> GEOID NAME county state geometry
#> <chr> <chr> <dbl> <dbl> <POINT [ft]>
#> 1 45009 Bamberg County, South Carolina 9 45 (1983425 502618.9)
#> 2 45001 Abbeville County, South Carolina 1 45 (1559068 872366.5)
#> 3 45005 Allendale County, South Carolina 5 45 (1890285 420377.9)
#> 4 45007 Anderson County, South Carolina 7 45 (1506653 981064.2)
#> 5 45021 Cherokee County, South Carolina 21 45 (1814307 1170306)
#> 6 45011 Barnwell County, South Carolina 11 45 (1866998 521545.1)
ggplot(county) +
geom_sf() +
geom_sf(data = count_centroid) +
theme_void()
Created on 2019-06-16 by the reprex package (v0.3.0)
Upvotes: 1