0

I have created a polygonal choropleth map in leaflet that toggles different geometries and legends on the regional and county (parish) level. The code works, but I made the addLayersControl toggle on the overlayGroups rather than the baseGroups. This resulted in a checkbox toggle rather than a binary radio button toggle. I used a JavaScript function to toggle the legends, and it works with the overlay groups, but I can't figure out how to hide and display the legend if I use radio buttons for base groups.

This code works for toggling the legend based on the checkbox selected. I would want to change the checkboxes to radio buttons in order for the map to have binary output.

library(leaflet.extras)

# Create palette and scale for map data for the parish level map.
parish_bins <- c(-16, -12, -8, -4, 0, 4, 8, 12, 16)
parish_pal <- colorBin("RdYlBu", domain = parish_sf$pct_change_2015_2021, bins = parish_bins)

# Create palette and scale for map data for the regional level map.
region_bins <- c(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5)
region_pal <- colorBin("RdYlBu", domain = region_sf$pct_change_2015_2021, bins = region_bins)

#Create labels for map data that include region names and values for the parish level map.
parish_labels <- sprintf(
  "<strong>%s</strong><br/><b>%g&#37;</b> change in population<br/>from 2015 to 2021.<br/><b>2015:</b> %g<br/><b>2021:</b> %g",
  parish_sf$ParishName, parish_sf$pct_change_2015_2021, parish_sf$data2015, parish_sf$data2021
) %>% lapply(htmltools::HTML)

#Create labels for map data that include region names and values for the regional level map.
region_labels <- sprintf(
   "<strong>%s</strong><br/><b>%g&#37;</b> change in population<br/>from 2015 to 2021.<br/><b>2015:</b> %g<br/><b>2021:</b> %g",
  region_sf$region_names, region_sf$pct_change_2015_2021, region_sf$data2015, region_sf$data2021
) %>% lapply(htmltools::HTML)


# Add polygons to leaflet map with binary toggle control.
map_with_toggle <- leaflet() %>%
  addTiles() %>%
  addPolygons(
    data = parish_sf,
    fillColor = ~parish_pal(parish_sf$pct_change_2015_2021),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlightOptions = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE
    ),
    label = parish_labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"
    ),
    group = "Population Change (Parish)"
  ) %>%
  addPolygons(
    data = region_sf,
    fillColor = ~region_pal(region_sf$pct_change_2015_2021),
    weight = 2,
    opacity = 1,
    color = "white",
    dashArray = "3",
    fillOpacity = 0.7,
    highlightOptions = highlightOptions(
      weight = 5,
      color = "#666",
      dashArray = "",
      fillOpacity = 0.7,
      bringToFront = TRUE
    ),
    label = region_labels,
    labelOptions = labelOptions(
      style = list("font-weight" = "normal", padding = "3px 8px"),
      textsize = "15px",
      direction = "auto"
    ),
    group = "Population Change (Region)"
  ) %>%
  addLegend(
    pal = parish_pal,
    values = parish_sf$pct_change_2015_2021,
    labFormat = labelFormat(suffix = "%", prefix = "(", digits = 1),
    title = "Percentage Change in Population (Parish)",
    opacity = 0.7,
    position = "bottomright",
    group = "Population Change (Parish)"
  ) %>%
  addLegend(
    pal = region_pal,
    values = region_sf$pct_change_2015_2021,
    labFormat = labelFormat(suffix = "%", prefix = "(", digits = 1),
    title = "Percentage Change in Population (Region)",
    opacity = 0.7,
    position = "bottomright",
    group = "Population Change (Region)"
  ) %>%
  addLayersControl(
    overlayGroups = c("Population Change (Parish)", "Population Change (Region)"),
    options = layersControlOptions(collapsed = FALSE, exclusive = TRUE)  # Set exclusive option to TRUE
  )

# ^ overlayGroups will change to baseGroups in the radio button format.

# JavaScript function to toggle legends
js_code <- '
function(map) {
  map.on("overlayadd", function(e){
    var layer = e.layer;
    var group = layer.options.group;
    if (group === "Population Change (Parish)") {
      map.showGroup("Population Change (Parish)");
      map.hideGroup("Population Change (Region)");
    } else if (group === "Population Change (Region)") {
      map.hideGroup("Population Change (Parish)");
      map.showGroup("Population Change (Region)");
    }
  });
}
'

# Use htmlwidgets::onRender to add the JavaScript code to the map
map_with_toggle <- htmlwidgets::onRender(map_with_toggle, js_code)

# Display the map with the binary toggle
map_with_toggle

Here's what the map looks like with the checkbox toggles

enter image description here

As I've noted in the code, overlayGroups would change to baseGroups, so I would need to create a completely new function to toggle the legends.

0

Browse other questions tagged or ask your own question.