如何下载纽约州所有县数据在R小册子地图

时间:2017-04-17 07:03:11

标签: r leaflet geojson ropensci

# 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复制的。

我已经知道如何按照代码中的要求下载纽约州县数据。(或者换句话说,如何生成nycounties.geojson文件)

我在前两个评论中浏览了两个网站,但未能将纽约州数据从美国的整个数据中分类。

1 个答案:

答案 0 :(得分: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