79792190

Date: 2025-10-16 13:27:44
Score: 0.5
Natty:
Report link

This article from the leaflet R package is a good example to use addLayersControl. Since the palette are already defined, it's straightforward, with one call of addPolygons for each map/year. Finally, htmlwidgets::saveWidget() allow to export the map into a file.

map1 = leaflet(st_trans ) |> 
  addPolygons( col = ~pal_2020(total_cat_2020), group= "2020")  |> 
  addPolygons( col = ~pal_2021(total_cat_2021), group= "2021")  |> 
  addPolygons( col = ~pal_2022(total_cat_2022), group= "2022")  |> 
  addLayersControl(baseGroups =c("2020","2021","2022"),
                   options = layersControlOptions(collapsed = FALSE))

htmlwidgets::saveWidget(map1, file="map1.html")
Reasons:
  • Blacklisted phrase (1): This article
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: VinceGreg