4

I want to use a base layer so that my filled polygons are in geographic context. However, my polygons have simply gone over the top of the base layer and therefore only bits and pieces of place names are visible. The image above shows this.

enter image description here

1) Is it possible to have the place names and detail over the top of my choropleth? 2) If 1 is not possible, can I remove the names and detail to make it cleaner?

leaflet(MyMapb) %>%
setView(-1.88, 55, 6) %>%
addControl(rr, position = "topleft") %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles("MapBox", options = providerTileOptions(
id = "mapbox.light",
accessToken = Sys.getenv('MAPBOX_ACCESS_TOKEN'))) %>%
#Polygon Layers
addPolygons(data = MyMapb,
          fillColor = ~pal1(MyMapb$eoi),
          fillOpacity = 1,
          color = "darkgrey",       ## color of borders between districts
          weight = 1.5,            ## width of borders
          popup = popup1,
          group = "EOIs") %>%
addPolygons(data = MyMapb, 
          fillColor = ~pal2(MyMapb$Awards),
          fillOpacity = 1,
          color = "darkgrey",       ## color of borders between districts
          weight = 1.5,            ## width of borders
          popup = popup2,
          group = "Awards") %>%

#Legend Layers
addLegend(pal = pal1,values = ~eoi, group = "EOIs", 
        position = "bottomleft") %>%
addLegend(pal = pal2,values = ~Awards, group = "Awards",
        position = "bottomright") %>%

#Layers Control
addLayersControl(overlayGroups = c("EOIs","Awards"),
               options = layersControlOptions(collapsed = FALSE)) %>% 
hideGroup("Awards")
addLayersControl(baseGroups = "CartoDB.PositronNoLabels",
               overlayGroups = c("map labels",
                                 "MyMapb"))
5

Using the latest leaflet version (2.0.1) allows to create custom map panes in order to control layer ordering. In combination with tile providers that serve base layers without labels and base layers with labels only, you can achieve what you desire.

library(leaflet)

options(viewer = NULL)

map = leaflet() %>%
  addMapPane(name = "polygons", zIndex = 410) %>% 
  addMapPane(name = "maplabels", zIndex = 420) %>% # higher zIndex rendered on top
  addProviderTiles("CartoDB.PositronNoLabels") %>%
  addProviderTiles("CartoDB.PositronOnlyLabels", 
                   options = leafletOptions(pane = "maplabels"),
                   group = "map labels") %>%
  addPolygons(data = gadmCHE, color = "black", weight = 1, 
              fillColor = "cornflowerblue", fillOpacity = 0.9,
              group = "gadmCHE",
              options = leafletOptions(pane = "polygons")) %>%
  addLayersControl(baseGroups = "CartoDB.PositronNoLabels",
                   overlayGroups = c("map labels",
                                     "gadmCHE"))

map

This way you can make sure that the labels are always rendered on top of the polygons (higher zIndex is rederned on top). For an overview of available tile providers have a look at http://leaflet-extras.github.io/leaflet-providers/preview/ where you will find some further labelless/labels-only providers that you can use.

NOTE: I am setting options(viewer = NULL) to ensure the map is opened in the browser as at the time of this writing RStudio has issues with rendering https served tiles. This should not be a problem if you use an RStudio version 1.2 or higher.

4
  • I love what you've done there and the aesthetic - brilliant! My only question is whether I can keep what've done whilst adding different selectable layers of my own? FYI I've included the bit of code above which then came after the first part. The idea is I have two variables I switch betwee.
    – Henry Dark
    Jun 8 '18 at 12:53
  • You can add as many overlaylayers as you like. In case you want specific ordering of those, just create new panes and pass those to the layers via the leafletOptions(pane = ...). Finally, add them via their group argument to the layersControl.
    – TimSalabim
    Jun 8 '18 at 13:26
  • great - one last question, how can I undo forcing R to open the HTML in browser? Long story short, I'm just trying to export my resulting html file but saveWidget fails with a pandoc out of memory error, and now I can't find any good way to export my file?
    – Henry Dark
    Jun 8 '18 at 14:33
  • To undo the viewer setting use options(viewer = rstudioapi::viewer). Your pandoc error is unrelated though. It usually means that the resulting html is too big. Try setting selfcontained = FALSE in the saveWidget call. This ususally resolves the issue.
    – TimSalabim
    Jun 8 '18 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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