简体   繁体   English

自定义shapefile中的R Highcharter贴图

[英]R Highcharter map from customized shapefile

I am having trouble importing and joining a geojson map to some data using the highcharter library. 我在使用highcharter库将geojson映射导入和加入到某些数据时遇到了麻烦。 I am trying to use a slim downed version of a sf dataset that I got using the tidycensus package which I then uploaded to https://mapshaper.org/ to reduce the size of the file by thinning out the polygons. 我正在尝试使用tidycensus软件包获得的sf数据集的精简版本,然后将其上传到https://mapshaper.org/,以通过减少多边形来减小文件的大小。 After thinning I exported as geojson and import into R. 细化后,我导出为geojson并导入到R中。

Here is an example. 这是一个例子。 First I download the data using tidycensus, create two data sets one for geometry and one for the attribute of interest, here its median family income. 首先,我使用tidycensus下载数据,创建两个数据集,一个用于几何图形,一个用于感兴趣的属性,这里是家庭收入中位数。 Then I export the geometry data to so that I can feed into mapshapper for reduction. 然后,我将几何数据导出到,以便可以导入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")

After reformatting in mapshaper I import back into R 重新格式化mapshaper后,我重新导入R

md_map_json <- jsonlite::fromJSON(txt = "FILEPATH/md_carto_tracts.json",simplifyVector = FALSE)


md_map_json <- geojsonio::as.json(md_map_json)

And then try and build a map based on an example from the highcharter docs here 然后尝试根据此处的highcharter文档中的示例构建地图

> 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")

The map actually renders and the census tracts are colored solid blue but the series data doesn't seem to successfully join even with or without using list_parse. 该地图实际上是渲染的,而人口普查区的颜色为纯蓝色,但是无论是否使用list_parse,系列数据似乎都无法成功加入。

在此处输入图片说明

I had the same problem, asked here: Make a choropleth from a non-highmap-collection map . 我遇到了同样的问题,在这里被问到: 从一个非highmap-collection地图中创建一个choropleth Nobody responded (I know!), so I finally got to a solution that I think should work for you too: 没有人回应(我知道!),所以我终于找到了一个我认为也应该对您有用的解决方案:

 #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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM