Tmap:无法创建国家级学区地图

Tmap: Cannot create school district map for national level

我想使用库 TmapR 的国家地图上绘制学区。我使用了来自 official website, the National Center for Education Statistics, for school district boundaries. Please note that I used the 2019 file instead of the latest one, and you can download this file from Single Composite File in the link mentioned earlier. I first tried with one state, Utah, and everything went well without any problems. My code to plot this map and the reference for the national map (from Bloomberg Citylab) 的 shapefile 如下;

library (sf)
library (tmap)
map <- st_read(".../schooldistrict_sy1819_tl19.shp")
ut <- map[map$STATEFP == 49, ]
tm_shape(ut) + tm_borders()

但是我在制作全国地图的时候遇到了错误。我使用了这段代码并得到了以下错误。

tm_shape(map) + tm_borders()
Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

当然,我做了一些研究。一个网站建议由于无效的几何图形而发生以下错误。我检查并试图使这些有效,但我犯了和第一次一样的错误。我试图用下面的代码解决这个问题。

sum(!st_is_valid(map$geometry))
[1] 858
st_make_valid(map$geometry)
Geometry set for 13315 features 
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -179.1686 ymin: -14.5487 xmax: 179.7487 ymax: 71.38961
Geodetic CRS:  NAD83
First 5 geometries:
MULTIPOLYGON (((-80.11417 26.07902, -80.11423 2...
MULTIPOLYGON (((-82.19919 26.77353, -82.19748 2...
MULTIPOLYGON (((-81.45356 25.80323, -81.45388 2...
MULTIPOLYGON (((-80.39962 25.25725, -80.40002 2...
MULTIPOLYGON (((-81.56409 27.34066, -81.56423 2...
 
tm_shape(map) + tm_borders()
Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

因为我对 R 中的几何结构一无所知,所以我完全不知道如何让它工作。如果以前有人这样做过,您能否就此分享任何意见/建议?解决此问题的任何想法将不胜感激。非常感谢您阅读我的问题!

感谢@mrhellmann,他发现了我犯的小错误,使用后需要将地图数据分配给新变量st_make_valid。我使用以下代码将 district-level 数据绘制到全国地图中。

library (SF)
library (tmap)
map <- st_read(".../schooldistrict_sy1819_tl19.shp")
map2 <- st_make_valid(map$geometry)
tm_shape(map2) + tm_borders()