Zooming in to view ZipCode using R Leaflet

I am using R leaftlet package to create an interactive chopopleth US

There are several tutorials on the Internet, and I can create an interactive state map with pop-ups and scaling. I also managed to create a separate zip-level map with pop-ups.

I would like both views to appear on the same map, but display a zip code when I increase the state or double-click on the state. For example, if I double-click on New York, a zip code in New York opens. Is there a package / feature in R that can help me do this?

Here are static screenshots of both to understand what I plan to integrate.

+6
source share
4 answers

I agree with Yehoshapat Schellekens that R cannot have the flexibility of a web programming language. But rarely, R is not flexible enough to achieve fantastic results! :) Here is an example of a "vanilla" of what you basically need. You can customize the popup using some JS.

library(shiny) library(leaflet) library(maps) library(maptools) library(sp) library(rgeos) mapStates = map("state", fill = TRUE, plot = FALSE) mapCounty = map("county", fill = TRUE, plot = FALSE) shinyApp( ui = fluidPage(leafletOutput('myMap'), br(), leafletOutput('myMap2')), server <- function(input, output, session) { output$myMap <- renderLeaflet({ leaflet() %>% addProviderTiles("Stamen.TonerLite", options = providerTileOptions(noWrap = TRUE)) %>% addPolygons(lng = mapStates$x, lat = mapStates$y, fillColor = topo.colors(10, alpha = NULL), stroke = FALSE) }) observeEvent(input$myMap_shape_click, { click <- input$myMap_shape_click if(is.null(click)) return() lat <- click$lat lon <- click$lng coords <- as.data.frame(cbind(lon, lat)) point <- SpatialPoints(coords) mapStates_sp <- map2SpatialPolygons(mapStates, IDs = mapStates$names) i <- point [mapStates_sp, ] selected <- mapStates_sp [i] mapCounty_sp <- map2SpatialPolygons(mapCounty, IDs = mapCounty$names) z <- over(mapCounty_sp, selected) r <- mapCounty_sp[(!is.na(z))] output$myMap2 <- renderLeaflet({ leaflet() %>% addProviderTiles("Stamen.TonerLite", options = providerTileOptions(noWrap = TRUE)) %>% addPolygons(data=r, fillColor = topo.colors(10, alpha = NULL), stroke = FALSE) }) }) }) 

NOTE. The datasets used in the example seem to have different accuracy (not ideal overlap for states and counties). Therefore, spatial comparisons take into account more districts than expected (those inside plus those that cross state borders). Use the name as an identifier to achieve a perfect match.

+5
source

You cannot create it through R, you need to run it through the good old java Script and, in particular, the booklet.

Keep in mind that R does not start the map, all it does is create a java-script HTML file template, your web browser will launch the rest (Not R interpreter)

The professional word you are looking for is event binding , which with one click will trigger both scaling on the original US map and opening a new state map with its zip code.

General instructions (this is all java Script, no R!):

go to http://leafletjs.com/reference.html and find the events you need the dblclick event.

Then you need to create a function that opens a new map.

Keep in mind that if you want to do complex things, R will give you very limited solutions, so my advice is when you need a good java script visualization, just go straight to the source :)

+3
source

Your requirement requires a lot of customization. If you are good at JavaScript, just check out geojson2svg , which gives a lot of flexibility. It basically converts GeoJSON to SVG, so that everything you need can be achieved with simple HTML and JavaScript. Here are some examples.

+1
source

I created the same type of application that worked out the G. Cocca code, and after a few months I messed with it again and again, I came up with a more elegant solution to your problem. For easy reproducibility, I use Rwanda shapefiles as an example (because they are much smaller than USA GADM shapefiles, but you can always replace them with your own US shapefiles).

 library(raster) library(shiny) library(leaflet) library(RColorBrewer) #load in shapefiles for state and county level states <- getData("GADM", country = "rwa", level = 1) counties <- getData("GADM", country = "rwa", level = 2) #define color palettes for states pal <- brewer.pal(8, "Dark2") statePal <- colorFactor(pal, states@data $NAME_1) shinyApp( ui = fluidPage( leafletOutput('myMap', width = "100%"), br(), leafletOutput("myMap2", width = "100%") ), #END UI server <- function(input, output, session){ #default state level map output output$myMap <- renderLeaflet({ leaflet() %>% addTiles() %>% addPolygons(data = states, fillColor = ~statePal( states@data $NAME_1), fillOpacity = 1, color = "white", stroke = T, weight = 1, layerId = states@data $NAME_1) #this sets the click id, very important! }) #END RENDERLEAFLET OUTPUT observeEvent(input$myMap_shape_click, { #define click object click <- input$myMap_shape_click #subset counties shapefile so that only counties from the clicked state are mapped selected <- counties[counties$NAME_1 == click$id,] #define color palette for counties countyPal <- colorFactor(pal, selected@data $NAME_2) #if click id isn't null (ie if ANY polygon is clicked on), draw map of counties if(!is.null(click$id)){ output$myMap2 <- renderLeaflet({ leaflet() %>% addTiles() %>% addPolygons(data = selected, fillColor = ~countyPal( selected@data $NAME_2), fillOpacity = 1, color = "white", stroke = T, weight = 1) }) #END RENDERLEAFLET } #END CONDITIONAL }) #END OBSERVE EVENT }) #END SHINYAPP 

The first conclusion is a map of your state level. Using this code, when you click on a state of interest, a click object is created that has click$id corresponding to the name of this state (which is set in the definition of layerId in the addPolygons call). With click$id as the selected state name, you can multiply your county county polygon with this state and plot it in the form of a map.

The design options for this card are actually endless. Hope this helps!

+1
source

All Articles