I am creating a leaflet map with two different types of layers:
- A layer for total rental households, and a layer for total borrowing households. This layer should be mutually exclusive: i.e., you should only be able to select one of these layers at a time (either show total rental households OR total borrowing households). This will effectively function as a base layer: you can only choose one.
- A layer Sydney, and a layer for the Rest of NSW. This layer does not need to be mutually exclusive: i.e., can show both Sydney AND Rest of NSW at the same time. These will effectively function like overlays: you can choose as many as you like.
Currently, I have only figured out how to show these layers as four separate layers: Sydney Renters, Rest of NSW renters, Sydney Borrowers, Rest of NSW Borrowers. (see image)
I would like to have these as two separate layers, with a choice for Renters OR Borrowers, and a separate layer for Sydney AND/OR Rest of NSW.
Here is the code used to generate the map:
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron, group = "OSM (default)") %>%
addPolygons(data = sf_syd, fillColor = ~pal2(Renting), color="white", weight=1, fillOpacity=0.4, group="Sydney Renters",
highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>%
addPolygons(data = sf_nsw, fillColor = ~pal2(Renting), color="white", weight=1, fillOpacity=0.4, group="Rest of NSW Renters",
highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>%
addPolygons(data = sf_syd, fillColor = ~pal2(Borrowing), color="white", weight=1, fillOpacity=0.4, group="Sydney Borrowers",
highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>%
addPolygons(data = sf_nsw, fillColor = ~pal2(Borrowing), color="white", weight=1, fillOpacity=0.4, group="Rest of NSW Borrowers",
highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>%
addLayersControl(baseGroups = c("OSM (default)"),
overlayGroups = c("Sydney Renters",
"Rest of NSW Renters",
"Sydney Borrowers",
"Rest of NSW Borrowers"),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(data= sf_syd, pal = pal2, values = ~perc_rental_stress, opacity = 0.7, position = "bottomright", title = "Households of </br> Household Type 'X'")