Reputation: 1546
I am getting an error in plotting the census tract of New York using tmap. Below is an example and the error.
It requires API key which is free. Here is the link - https://api.census.gov/data/key_signup.html
library(tidycensus)
library(tmap)
census_api_key("it requires API key")
NY <- get_acs(
geography = "tract",
table = "B19001",
state = "NY",
year = 2020,
geometry = TRUE,
cache_table = TRUE
)
tm_shape(shp = NY) +
tm_polygons()
I am getting an error.
Error in `$<-.data.frame`(`*tmp*`, "SHAPE_AREAS", value = c(5.02814518540036, :
replacement has 91698 rows, data has 91987
In addition: Warning message:
The shape NY contains empty units.
Upvotes: 1
Views: 391
Reputation: 5489
It looks like get_acs()
returns 289 empty geometries which causes the tmap
error. Using sf::st_make_valid
takes care of the error, though it still causes a warning.
More discussion on empty geometries and what to do about them here: https://r-spatial.org/r/2017/03/19/invalid.html#empty-geometries
NY <- get_acs(
geography = "tract",
table = "B19001",
state = "NY",
year = 2020,
geometry = TRUE,
cache_table = TRUE
)
tm_shape(shp = st_make_valid(NY)) + ### <- added st_make_valid here
tm_polygons()
Upvotes: 2