我在用tmap绘制纽约的人口普查图时出错了。下面是一个例子和错误。
它要求API密钥是免费的。下面是链接- 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()我搞错了。
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. 发布于 2022-10-09 13:57:11
看起来get_acs()返回289个空几何图形,这会导致tmap错误。使用sf::st_make_valid可以处理错误,尽管它仍然会引起警告。
关于空几何图形的更多讨论,以及如何处理它们: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()

https://stackoverflow.com/questions/73990261
复制相似问题