简体   繁体   中英

R Highcharter map from customized shapefile

I am having trouble importing and joining a geojson map to some data using the highcharter library. 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. After thinning I exported as geojson and import into 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. Then I export the geometry data to so that I can feed into mapshapper for reduction.

#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

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

> 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.

在此处输入图片说明

I had the same problem, asked here: Make a choropleth from a non-highmap-collection map . 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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