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

The above code is copied from https://rstudio.github.io/leaflet/json.html.

I have on idea how to download the NY State County data as required in the code.(or in other words, how to produce nycounties.geojson file)

I went over both websites in the first two comments but failed to subset the NY state data from the whole data in the US.

1
  • so, di you find out how to get the data? Commented Feb 9, 2019 at 6:38

1 Answer 1

2

After downloading the 22 MB json file, I do this and it appears to work.

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

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.