【发布时间】:2020-05-30 16:37:40
【问题描述】:
我正在尝试创建每个州所有学区的地图。以下代码适用于所有州,佛罗里达州除外,我收到此错误: data.frame(..., check.names = FALSE) 中的错误: 参数暗示不同的行数:67、121
require(dplyr)
require(sf)
library(tmap)
require(lwgeom)
temp <- tempfile() ### create a temporary file to download zip file to
temp2 <- tempfile() ### create a temporary file to put unzipped files in
download.file("https://s3.amazonaws.com/data.edbuild.org/public/Processed+Data/SD+shapes/2018/shapefile_1718.zip", temp) # downloading the data into the tempfile
unzip(zipfile = temp, exdir = temp2) # unzipping the temp file and putting unzipped data in temp2
filename <- list.files(temp2, full.names = TRUE) # getting the filename of the downloaded data
shp_file <- filename %>%
subset(grepl("*.shp$", filename)) ## selecting only the .shp file to read in
state_shape <- sf::st_read(shp_file) %>% ## reading in the downloaded data
dplyr::mutate(GEOID = as.character(GEOID),
GEOID = stringr::str_pad(GEOID, width = 7, pad = "0")) %>%
filter(State == "Florida")
url = "https://s3.amazonaws.com/data.edbuild.org/public/Processed+Data/Master/2017/full_data_17_geo_exc.csv"
master <- read.csv(file = url, stringsAsFactors = FALSE) %>%
dplyr::mutate(NCESID = as.character(NCESID),
NCESID = stringr::str_pad(NCESID, width = 7, pad = "0"),
year = "2017") %>%
dplyr::select(-NAME, -State, -STATE_FIPS) ## removing variables that duplicate with shapes
state_shape <- state_shape %>%
dplyr::left_join(master, by = c("GEOID" = "NCESID")) %>%
select(GEOID, NAME, State, StPovRate)
shape.clean <- lwgeom::st_make_valid(state_shape) # making all geometries valid
povertyBlues <- c('#dff3fe', '#92DCF0', '#49B4D6', '#2586a5', '#19596d')
map <- tm_shape(shape.clean) +
tm_fill("StPovRate", breaks=c(0, .1, .2, .3, .4, 1), title = "Student Poverty",
palette = povertyBlues,
legend.format=list(fun=function(x) paste0(formatC(x*100, digits=0, format="f"), " %"))) +
tm_shape(shape.clean) +
tm_borders(lwd=.25, col = "#e9e9e9", alpha = 1) +
tm_layout(inner.margins = c(.05,.25,.1,.05))
map ### view the map
tm_shape$shp 和 state_shape 的长度都是 67。有谁知道是什么原因导致“参数暗示不同的行数:67、121”?
谢谢!!
【问题讨论】:
-
欢迎来到 Stack Overflow。当您提出任何问题时,您希望提供所有数据以及您的代码。现在,您缺少
shape.clean,我相信您的数据包含StPovRate。请提供此数据。否则,没有人可以复制您的情况并思考如何帮助您。 -
state_shape 的几何形状无效;这可以通过
lwgeom::st_make_valid()修复。但是:下载的文件(对于尝试弱连接的人来说是 28 mb)不包含“StPovRate”字段。 -
我编辑了我的问题,为疏忽添加 StPovRatem 道歉。 Jindra,您是正确的 state_shape 具有无效的几何形状,但不幸的是 st_make_valid 没有修复它。为了清楚起见,我将其添加到示例中。