rgistmap

Creating custom control to view both layers and metrics in GIS map using tmap in R


I am trying to create an interactive GIS map in R that allows users to do two things:

  1. Select individual measures to view.
  2. View measures for sub-groups within a geographic area

For example, I would like to view diabetes prevalence for communities around Oxford. I'd then like to see what diabetes prevalence is for the region that those communities comprise. I'd then like to see average waiting times for procedures in Yorkshire, and then communities in this area.

I am using customised shapefiles for my maps, so I don't reproducible code that I can share -- however, I can explain what I've done, share the code that produces the map and show a screenshot of the output map. This map shows health care geographic areas in England: Integrated Care Boards (ICBs) and Clinical Commissioning Groups (CCGs) within them. It also has two generic metrics with dummy data.

Below is the code I used: tm_shape(england_ICB_maps) + tm_polygons(col=c('Metric 1', 'Metric 2')) + tm_shape(england_CCG_maps) + tm_polygons(col=c('Metric 1', 'Metric 2')) + tm_facets(as.layers = TRUE)

As shown in the GIS map, I see both metrics, reported twice: One for each layer (ICB and CCG). I can select the metric, but it appears for both layers -- and I cannot select a layer.

The interactive map uses Leaflet (as shown in the screenshot), and there is flexibility in Leaflet to adjust the control -- for example, specifying OpenStreetMap as the default Basemap, and removing other options.

In tm_view, there is an option for leaflet.options -- but the guidance for using this in tm_view is not clear; it provides no example for how to enter this in. The documentation link can be found below:

https://r-tmap.github.io/tmap/reference/tm_view.html

The guidance says:

Other options passed on via leafletOptions to leaflet.js map creation (see leaflet, follow Docs, Map, Creation). Named list, where the names correspond to the variable names. Tip: use zoomSnap and zoomDelta for fractional zooming.

However, I'm not clear what this is actually saying, and how to set this up.

I tried adding this to the code above: + tm_view(leaflet.options = addLayersControl(baseGroups = "OpenStreetMap"))

I got the following error: Error in addLayersControl(baseGroups = "OpenStreetMap") : could not find function "addLayersControl"

Can anyone advise whether it's possible to do what I'm hoping to do? Also, could someone provide an example of how to use the leaflet.options option in tm_view?

Thank you!

sample GIS map


Solution

  • Three years too late, but this might help someone down the mostly undocumented rabbit hole that is tmap_mode("view")

    Also, could someone provide an example of how to use the leaflet.options option in tm_view?

    I'm fairly sure addLayersControl isn't supported as an option in leaflet.options, but here's a working example of how to use leaflet.options to change the padding in view mode:

    # world choropleth/bubble map of the world
    data(World, metro)
    metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100
    
    tmap_mode("view")
    
    map1 <- tm_shape(metro) +
        tm_bubbles("pop2010", col = "growth", 
            border.col = "black", border.alpha = .5, 
            style="fixed", breaks=c(-Inf, seq(0, 6, by=2), Inf),
            palette="-RdYlBu", contrast=1, 
            title.size="Metro population", 
            title.col="Growth rate (%)", id="name", 
                popup.vars=c("pop2010", "pop2020", "growth")) + 
        tm_legend(outside=TRUE) +
        tm_view(leaflet.options=(padding = 3))
    
    map1
    
    

    Update 27/7/23

    Solution: Combining the output of tmap with leaflet gives a lot more control over aesthetics.

    Here's a working approach to leverage addLayersControl via library(leaflet). The attached example shows how to set the layersControlOptions to keep the control panel open at all times, and uses hideGroup to select which layers are active when the map first opens.

    # world choropleth/bubble map of the world
    
    library(tmap)
    library(leaflet)
    
    data(World, metro)
    metro$growth <- (metro$pop2020 - metro$pop2010) / (metro$pop2010 * 10) * 100
    
    tmap_mode("view")
    
    map1 <- tm_shape(metro) +
        tm_bubbles("pop2010", col = "growth", 
            border.col = "black", border.alpha = .5, 
            style="fixed", breaks=c(-Inf, seq(0, 6, by=2), Inf),
            palette="-RdYlBu", contrast=1, 
            title.size="Metro population", 
            title.col="Growth rate (%)", id="name", 
                popup.vars=c("pop2010", "pop2020", "growth")) + 
        tm_legend(outside=TRUE) +
        tm_view(leaflet.options=(padding = 3))
    
    map1 %>% tmap_leaflet() %>%
        addLayersControl(position="topleft", overlayGroups=c("pop2010"),
          options=layersControlOptions(collapsed = FALSE)) %>%
      leaflet::hideGroup("pop2010")