2017-04-17 109 views
-1
# From http://eric.clst.org/Stuff/USGeoJSON and 
# https://en.wikipedia.org/wiki/List_of_United_States_counties_and_county_equivalents 
nycounties <- geojsonio::geojson_read("json/nycounties.geojson", 
    what = "sp") 
# Or use the rgdal equivalent: 
# nycounties <- rgdal::readOGR("json/nycounties.geojson", "OGRGeoJSON") 

pal <- colorNumeric("viridis", NULL) 

leaflet(nycounties) %>% 
    addTiles() %>% 
    addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1, 
    fillColor = ~pal(log10(pop)), 
    label = ~paste0(county, ": ", formatC(pop, big.mark = ","))) %>% 
    addLegend(pal = pal, values = ~log10(pop), opacity = 1.0, 
    labFormat = labelFormat(transform = function(x) round(10^x))) 

以上代碼複製自https://rstudio.github.io/leaflet/json.html如何在R下載NY州所有郡縣數據用於傳單地圖

我有想法如何在代碼需要下載紐約州郡數據(或者換句話說,如何生產nycounties.geojson文件)

我去了這兩個網站在第一兩點意見但未能將美國紐約州的數據納入整個數據中。

回答

2

下載22 MB json文件後,我這樣做,它似乎工作。

library(leaflet) 

xy <- geojsonio::geojson_read("gz_2010_us_050_00_500k.json", what = "sp") 

> names(xy) 
[1] "GEO_ID"  "STATE"  "COUNTY"  "NAME"  "LSAD"  "CENSUSAREA" 

# from Wikipedia list of counties, find Genesse county, 
# which should be located in NY state 
> xy[grepl("36037", xy$GEO_ID), ]$STATE 
[1] 36 

# NY state should be number 36 

nyc <- xy[xy$STATE == 36, ] 

leaflet(nyc) %>% 
    addTiles() %>% 
    addPolygons() 

enter image description here

相關問題