使用通过 tidycensus 下载的数据的 dotsInPolys 长度不匹配

dotsInPolys length mismatch using data downloaded via tidycensus

你能帮忙找出解决 dotsInPolys 抛出的长度不匹配错误的最佳方法吗?我认为这是因为多边形数据中存在 NA 或 NULL 或某些问题导致它太长。这是重现错误的代码。最终,我想使用 Leaflet 绘制多场比赛,但此时我无法生成随机点所需的 lat/lon。

require(maptools)
require(tidycensus)

person.number.divider <- 1000

census_api_key("ENTER KEY HERE", install = TRUE)

racevars <- c(White = "B02001_002", #"P005003" 
              Black = "B02001_003", #Black or African American alone
              Latinx = "B03001_003"
)

nj.county <- get_acs(geography = "county", #tract
              year = 2015,
              variables = racevars,
              state = "NJ", #county = "Harris County",
              geometry = TRUE,
              summary_var = "B02001_001")

library(sf)
st_write(nj.county, "nj.county.shp", delete_layer = TRUE)

nj <- rgdal::readOGR(dsn = "nj.county.shp") %>%
  spTransform(CRS("+proj=longlat +datum=WGS84"))

nj@data <- nj@data %>% 
  tidyr::separate(NAME,
                  sep =",",
                  into = c("county", "state"))  %>%
  dplyr::select(estimat,variabl, GEOID, county) %>%
  spread(key = variabl, value = estimat) %>%
  mutate(county = trimws(county))


black.dots <- dplyr::select(nj@data, Black) / person.number.divider #%>%
black.dots <-   dotsInPolys(nj, as.integer(black.dots$Black), f="random")

# Error in dotsInPolys(nj, as.integer(black.dots$Black), f = "random") : 
# different lengths

length(nj) # 63 This seems too many, because I believe NJ has 21 counties.
length(black.dots$Black) # 21

这个 post () 几乎可以帮助我,但我不知道如何将它应用到我的案例中。

我可以通过删除 NA 和黑色 pop 大于 0 的县来更改 nj spatialpolygonsdataframe 的长度,但是地图不会绘制多个县(也许人口普查下载有问题?)。

看起来您可能已经弄明白了,但我想分享另一种使用 sf::st_sample() 而不是 maptools::dotsInPolys() 的方法。这样做的一个优点是您不需要将从 tidycensus 获得的 sf 对象转换为 sp 对象。

在下面的示例中,我按种族将人口普查数据拆分为三个 sf 对象的列表,然后对列表的每个元素(每个种族)执行 st_sample()。接下来,我将采样点重新组合为一个 sf 对象,每个点都有一个新的竞赛变量。最后,我使用 tmap 制作地图,尽管您也可以使用 ggplot2leaflet 制作地图。

library(tidyverse)
library(tidycensus)
library(sf)
library(tmap)

person.number.divider <- 1000

racevars <- c(White = "B02001_002", #"P005003" 
              Black = "B02001_003", #Black or African American alone
              Latinx = "B03001_003"
              )

# get acs data with geography in "tidy" form
nj.county <- get_acs(geography = "county", #tract
                     year = 2015,
                     variables = racevars,
                     state = "NJ", #county = "Harris County",
                     geometry = TRUE,
                     summary_var = "B02001_001"
                     )

# split by race
county.split <- nj.county %>% 
  split(.$variable)

# randomly sample points in polygons based on population
points.list <- map(county.split, ~ st_sample(., .$estimate / person.number.divider))

# combine points into sf collections and add race variable 
points <- imap(points.list, ~ st_sf(tibble(race = rep(.y, length(.x))), geometry = .x)) %>% 
  reduce(rbind)

# map!
tm_shape(nj.county) +
  tm_borders(col = "darkgray", lwd = 0.5) +
  tm_shape(points) +
  tm_dots(col = "race", size = 0.01, pal = "Set2")

我没有足够的代表直接 post 地图图像,but here it is