我在使用highcharter库将geojson映射导入和连接到某些数据时遇到了麻烦。我正在尝试使用tidycensus软件包获得的sf数据集的精简版本,然后将其上传到distorted/warped,以通过减少多边形来减小文件的大小。细化后,我导出为geojson并导入到R中。
这里是一个例子。首先,我使用tidycensus下载数据,创建两个数据集,一个用于几何图形,一个用于感兴趣的属性,这里是家庭收入中位数。然后,我将几何数据导出到,以便可以导入mapshapper进行缩减。
#start with an example for one state
##pull geometry data for one state
md_data <- get_acs(geography = "tract",
state = "MD",
variables = "B19113_001",
geometry = T,
key = Sys.getenv("CENSUS_API_KEY"))
#data set of just GEOID and median family income for use in mapping
md_mfi <- as.data.frame(md_data) %>%
mutate(median_family_income = case_when(is.na(estimate) ~ 0,
TRUE ~ estimate)) %>%
select(GEOID,median_family_income)
#slim down to just the geoid and the geometry data
md_tracts <- md_data %>%
select(GEOID,geometry)
st_write(md_tracts, "U:/M1JPW00/GeoSpatial/census_tracts/acs_carto_2016/md_carto_tracts.shp")
在重新设置mapshaper格式后,我重新导入R
md_map_json <- jsonlite::fromJSON(txt = "FILEPATH/md_carto_tracts.json",simplifyVector = FALSE)
md_map_json <- geojsonio::as.json(md_map_json)
然后尝试根据highcharter文档https://mapshaper.org/
中的示例构建地图> class(md_map_json)
[1] "json" "geo_json"
> head(md_mfi)
GEOID median_family_income
1 24001000100 54375
2 24001000200 57174
3 24001000300 48362
4 24001000400 52038
5 24001000500 46174
6 24001000600 49784
highchart(type = "map") %>%
hc_add_series(mapData = md_map_json,
data = list_parse(md_mfi),
joinBy = "GEOID",
value = "median_family_income",
name = "Median Family Income")
地图实际上已渲染,人口普查区被涂成纯蓝色,但是无论是否使用list_parse,系列数据似乎都无法成功加入。
答案 0 :(得分:0)
我有同样的问题,在这里问: Make a choropleth from a non-highmap-collection map。没有人回应(我知道!),所以我终于找到了一个我认为也应该对您有用的解决方案:
#Work with the map you get until this step:
md_map_json <- jsonlite::fromJSON(txt = "FILEPATH/md_carto_tracts.json",simplifyVector = FALSE)
#This part is unnecessary:
#md_map_json <- geojsonio::as.json(md_map_json)
#Then, write your map like this:
highchart() %>%
hc_add_series_map(md_map_json, md_mfi, value = "median_family_income", joinBy = "GEOID")