McMahok
McMahok

Reputation: 360

Layering maps in leaflet

Let's say I make the following map in leaflet in R using


dput(states)
structure(list(Nestbox = c("C1", "C1", "C1", "C1", "C1", "C1", 
"C1", "C1", "C10", "C10", "C10", "C10", "C10", "C10", "C10", 
"C10", "C101", "C101", "C101", "C101", "C101", "C101", "C101", 
"C101", "C102", "C102", "C102", "C102", "C102", "C102", "C102", 
"C102", "C103", "C103", "C103", "C103", "C103", "C103", "C103", 
"C103", "C104", "C104", "C104", "C104", "C104", "C104", "C104", 
"C104", "C105", "C105", "C105", "C105", "C105", "C105", "C105", 
"C105", "C105", "C106", "C106", "C106", "C106", "C106", "C106", 
"C106", "C106", "C107", "C107", "C107", "C107", "C107", "C107", 
"C107", "C107", "C108", "C108", "C108", "C108", "C108", "C108", 
"C108", "C108", "C109", "C109", "C109", "C109", "C109", "C109", 
"C109", "C109", "C11", "C11", "C11", "C11", "C11", "C11", "C11", 
"C11", "C110", "C110", "C110"), State.code = c(0L, 0L, 0L, 2L, 
4L, 3L, 3L, 4L, 0L, 0L, 0L, 1L, 1L, 3L, 4L, 4L, 0L, 0L, 0L, 2L, 
4L, 3L, 4L, 4L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 2L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 2L, 2L, 3L, 4L, 0L, 0L, 0L, 3L, 4L, 4L, 4L, 4L, 0L, 0L, 0L, 
0L, 0L, 2L, 4L, 4L, 0L, 0L, 0L, 2L, 4L, 4L, 4L, 4L, 0L, 2L, 4L
), lon = c(-1.329996387, -1.329996387, -1.329996387, -1.329996387, 
-1.329996387, -1.329996387, -1.329996387, -1.329996387, -1.332834197, 
-1.332834197, -1.332834197, -1.332834197, -1.332834197, -1.332834197, 
-1.332834197, -1.332834197, -1.330372664, -1.330372664, -1.330372664, 
-1.330372664, -1.330372664, -1.330372664, -1.330372664, -1.330372664, 
-1.33032833, -1.33032833, -1.33032833, -1.33032833, -1.33032833, 
-1.33032833, -1.33032833, -1.33032833, -1.330002568, -1.330002568, 
-1.330002568, -1.330002568, -1.330002568, -1.330002568, -1.330002568, 
-1.330002568, -1.330772917, -1.330772917, -1.330772917, -1.330772917, 
-1.330772917, -1.330772917, -1.330772917, -1.330772917, -1.331053618, 
-1.331053618, -1.331053618, -1.331053618, -1.331053618, -1.331053618, 
-1.331053618, -1.331053618, -1.331053618, -1.331627637, -1.331627637, 
-1.331627637, -1.331627637, -1.331627637, -1.331627637, -1.331627637, 
-1.331627637, -1.331746318, -1.331746318, -1.331746318, -1.331746318, 
-1.331746318, -1.331746318, -1.331746318, -1.331746318, -1.331257985, 
-1.331257985, -1.331257985, -1.331257985, -1.331257985, -1.331257985, 
-1.331257985, -1.331257985, -1.333494628, -1.333494628, -1.333494628, 
-1.333494628, -1.333494628, -1.333494628, -1.333494628, -1.333494628, 
-1.331850567, -1.331850567, -1.331850567, -1.331850567, -1.331850567, 
-1.331850567, -1.331850567, -1.331850567, -1.332551665, -1.332551665, 
-1.332551665), lat = c(51.75921563, 51.75921563, 51.75921563, 
51.75921563, 51.75921563, 51.75921563, 51.75921563, 51.75921563, 
51.75639953, 51.75639953, 51.75639953, 51.75639953, 51.75639953, 
51.75639953, 51.75639953, 51.75639953, 51.75924475, 51.75924475, 
51.75924475, 51.75924475, 51.75924475, 51.75924475, 51.75924475, 
51.75924475, 51.75892126, 51.75892126, 51.75892126, 51.75892126, 
51.75892126, 51.75892126, 51.75892126, 51.75892126, 51.7584159, 
51.7584159, 51.7584159, 51.7584159, 51.7584159, 51.7584159, 51.7584159, 
51.7584159, 51.75824945, 51.75824945, 51.75824945, 51.75824945, 
51.75824945, 51.75824945, 51.75824945, 51.75824945, 51.75788241, 
51.75788241, 51.75788241, 51.75788241, 51.75788241, 51.75788241, 
51.75788241, 51.75788241, 51.75788241, 51.75727427, 51.75727427, 
51.75727427, 51.75727427, 51.75727427, 51.75727427, 51.75727427, 
51.75727427, 51.75708613, 51.75708613, 51.75708613, 51.75708613, 
51.75708613, 51.75708613, 51.75708613, 51.75708613, 51.75667003, 
51.75667003, 51.75667003, 51.75667003, 51.75667003, 51.75667003, 
51.75667003, 51.75667003, 51.75680788, 51.75680788, 51.75680788, 
51.75680788, 51.75680788, 51.75680788, 51.75680788, 51.75680788, 
51.75629504, 51.75629504, 51.75629504, 51.75629504, 51.75629504, 
51.75629504, 51.75629504, 51.75629504, 51.75689245, 51.75689245, 
51.75689245), April.day = c(-27, -14, -9, -2, 3, 7, 12, 20, -27, 
-14, -9, -2, 3, 7, 12, 20, -27, -14, -9, -2, 3, 7, 12, 20, -27, 
-14, -9, -2, 3, 7, 12, 20, -27, -14, -9, -2, 3, 7, 12, 20, -27, 
-14, -9, -2, 3, 7, 12, 20, -27, -14, -9, -2, 3, 7, 12, 20, 23, 
-27, -14, -9, -2, 3, 7, 12, 20, -27, -14, -9, -2, 3, 7, 12, 20, 
-27, -14, -9, -2, 3, 7, 12, 20, -27, -14, -9, -2, 3, 7, 12, 20, 
-27, -14, -9, -2, 3, 7, 12, 20, -27, -14, -9), Inc = c("0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "1", "0", "0", "0")), row.names = c(NA, 100L), class = "data.frame")
library(tidyverse)
library(leaflet)

statecol<- colorFactor(palette = "viridis", states$Inc) #create the colour palette

leaflet() %>% 
    setView(lng = -1.324640, lat = 51.770462, zoom = 13.25) %>%
    addTiles() %>%
    addCircleMarkers(data = states, label = ~as.character(states$Nestbox), radius = 5, color = ~statecol(Inc), stroke = FALSE, fillOpacity = 5)
    

I want to keep this map and add another similar layer again using a different variable e.g in this example State.code So that I can map these on top of the existing map. So I have all the circle markers from the previous map and that I can alter based on the new variable of interest?

Is that possible in leaflet, I can't seem to find an answer

Upvotes: 0

Views: 391

Answers (1)

Susan Switzer
Susan Switzer

Reputation: 1922

I am sure if this example realizes your vision Include the group argument for each layer of the map, then feed the group names to the addLayerControl function. Additional examples at https://rstudio.github.io/leaflet/showhide.html


library(tidyverse)
library(leaflet)

statecol<- colorFactor(palette = "viridis", states$Inc) #create the colour palette

leaflet() %>% 
  setView(lng = -1.324640, lat = 51.770462, zoom = 13
          ) %>%
  addTiles() %>%
  addCircleMarkers(data = states, 
                   label = ~as.character(states$Nestbox), 
                   radius = 5, 
                   color = ~statecol(Inc), 
                   stroke = FALSE, 
                   fillOpacity = 5, 
                   group = 'nestbox') %>% 
  addMarkers(lng = states$lon, 
                   lat = states$lat,
                   popup = states$State.code, 
                   group = 'statecode') %>% 
  addLayersControl(
    baseGroups = c("nestbox", "statecode"),
    options = layersControlOptions(collapsed = FALSE)
  )

sample

Upvotes: 1

Related Questions