4

Is it possible to style a TopoJSON file from its features for a choropleth using R/leaflet? Tried a few things, and I'm not sure if this is impossible with the leaflet package or if I just don't have the syntax right, especially accessing the properties to enter in the pal() function. Here's what I have:

pal<-colorNumeric(palette ="YlOrRd",domain = USAdata$GINI) #USAdata data frame I merged with the spdf before converting it to shp/topojson

map<-leaflet() %>% 
  addTiles(options=tileOptions(minZoom = 3)) %>% 
  setMaxBounds(-167.276413,5.499550,-52.233040, 83.162102) %>%
  setView(93.85,37.45,zoom =3) %>%
  #addGeoJSON(geojson = jso5)
  addTopoJSON(topojson=jso, fillColor = ~pal("GINI"))
#addPolygons(data=poly)

this throws up an error:

"Error in UseMethod("doResolveFormula") : 
  no applicable method for 'doResolveFormula' applied to an object of class "NULL""

I also tried converting it to an R object the topojson with fromJSON() and adding style elements, but this won't load after I try send it back with toJSON().

Not sure if relevant, but the topojson was created from a shapefile made following the instructions here:

with cl:

topojson -o 'USApuma.json' --shapefile-encoding utf8 --id-property=+GEOID10 -p GINI,+STATEFP10,+GEOID10 -- 'usaetest.shp'

then read in with readLines().

Eventually trying to throw this into a shiny app. Here's some examples I've been following.

Nathan Thompson
  • 335
  • 1
  • 10

1 Answers1

1

Do you need to use TopoJSON? If not consider using the tigris package (disclosure: I created and maintain the package). It'll get you access to just about any Census geographic dataset you need, and plays nicely with leaflet. Here's a brief example in line with what you are doing. For example, you can get all PUMAs in the continental US with the following code:

library(readr)
library(tigris)
library(leaflet)

us_states <- unique(fips_codes$state)[1:51]

continental_states <- us_states[!us_states %in% c("AK", "HI")]

pumas_list <- lapply(continental_states, function(x) {
  pumas(state = x, cb = TRUE)
})

us_pumas <- rbind_tigris(pumas_list)

I've generated a sample dataset that measures PUMA median household income for this example; the geo_join function from the tigris package can merge the dataset to the spatial data frame us_pumas:

puma_income <- read_csv('http://personal.tcu.edu/kylewalker/data/puma_income.csv')

joined_pumas <- geo_join(us_pumas, puma_income, 'GEOID10', 'GEOID')

We can then plot with Leaflet:

pal <- colorQuantile(palette = 'YlOrRd', domain = joined_pumas$hhincome, n = 7)

leaflet(joined_pumas) %>% 
  addProviderTiles('CartoDB.Positron') %>% 
  addPolygons(weight = 0.5, fillColor = ~pal(hhincome), 
              color = 'lightgrey', fillOpacity = 0.75, 
              smoothFactor = 0.2) %>% 
  addLegend(pal = pal, 
            values = joined_pumas$hhincome)

PUMA median household income

If you are planning to build a Shiny app, I'd recommend saving out the PUMAs you obtain from tigris first as a .rda file and reading it in with your Shiny script so you don't have to rbind_tigris every time.

kwalkertcu
  • 1,011
  • 6
  • 8
  • 2
    The motivation for using TopoJSON is speed. I've implemented it with an SPDF as you do above, but it's very slow. re: **tigris** I like the package, I just happened to have the data and .shp s already on my drive. I use the `geo_join` to merge the spdf & the data, then I convert to TopoJSON with the command line utility. Leaflet draws the Topo just fine, but but I can't seem to color it with the Topo's attributes--passing a ~pal function to `addTopoJSON` 's `fillColor` argument doesn't work. Again, I'm not sure if this a syntax thing or if it's impossible with `addTopoJSON` – Nathan Thompson Jan 23 '16 at 19:47