Using the "leaflet" library, I made the following 3 maps:
#load libraries
library(dplyr)
library(leaflet)
library(geosphere)
library(leafsync)
library(mapview)
##map 1
map_data_1 <- data.frame("Lat" = rnorm(5, 43,1), "Long" = rnorm(5, -79,1), type = c(1,2,3,4,5))
map_data_1$type = as.factor(map_data_1$type)
leaflet(map_data_1) %>%
addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE))
m1 = leaflet(map_data_1) %>% addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, color = ~ifelse(type==1,"red","blue"), labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE))
##map 2
library(dplyr)
library(leaflet)
map_data_2 <- data.frame("Lat" = rnorm(5, 43,1), "Long" = rnorm(5, -79,1), type = c(1,2,3,4,5))
map_data_2$type = as.factor(map_data_2$type)
leaflet(map_data_2) %>%
addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE))
m2 = leaflet(map_data_2) %>% addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, color = ~ifelse(type==1,"red","blue"), labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE))
##map 3
library(dplyr)
library(leaflet)
map_data_3 <- data.frame("Lat" = rnorm(5, 43,1), "Long" = rnorm(5, -79,1), type = c(1,2,3,4,5))
map_data_3$type = as.factor(map_data_3$type)
leaflet(map_data_3) %>%
addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE))
m3 = leaflet(map_data_3) %>% addTiles() %>% addCircleMarkers(stroke = FALSE, label = ~type,fillOpacity = 0.8, color = ~ifelse(type==1,"red","blue"), labelOptions = labelOptions(direction = "center",style = list('color' = "white"),noHide = TRUE, offset=c(0,0), fill = TRUE, opacity = 1, weight = 10, textOnly = TRUE))
In this question over here (Join two maps made in the leaflet in R), I learned how to "sync" these 3 maps:
# sync maps: (link for how to save final synched map as a html file https://github.com/r-spatial/mapview/issues/35)
m4 = sync(m1,m2, m3, ncol = 3)
My Question: Instead of these 3 maps being "synched" - I would like to make a single map that includes all 3 maps as "layers", which allows you to "toggle" between between these 3 maps. This would look something like this:
I found this link over here that shows how to create layers in a leaflet map: https://poldham.github.io/abs/mapgbif.html
But this creates layers for different "types" of points - not for different maps. I suppose I could "adapt" my code, merge all 3 files into a single file and label them (with a new label type variable) accordingly:
map_data_1$layer_type = as.factor(1)
map_data_2$layer_type = as.factor(2)
map_data_3$layer_type = as.factor(3)
final_map_data = rbind(map_data_1, map_data_2, map_data_3)
library(RColorBrewer)
my_palette <- brewer.pal(9, "Paired")
factpal <- colorFactor(my_palette, levels = final_map_data$layer_type)
m = leaflet(final_map_data) %>% addTiles() %>% addCircleMarkers(~Long,
~Lat, popup = final_map_data$layer_type, radius = 1, weight = 2, opacity = 0.5,
fill = TRUE, fillOpacity = 0.2, color = ~factpal(layer_type))
groups = unique(final_map_data$layer_type)
map = leaflet(final_map_data) %>% addTiles(group = "OpenStreetMap")
for (i in groups) {
data = final_map_data[final_map_data$layer_type == i, ]
map = map %>% addCircleMarkers(data = data, ~Long, ~Lat, radius = 1,
weight = 2, opacity = 0.5, fill = TRUE, fillOpacity = 0.2, color = ~factpal(layer_type),
group = i)
}
map %>% addLayersControl(overlayGroups = groups, options = layersControlOptions(collapsed = FALSE))
Unfortunately, the above code created a map with empty layers:
Can someone please show me how to directly combine these three maps (m1, m2, m3) into a single map with layers?
** Note: I would like to keep the exact same coloring scheme for all maps : first point "red", all other points "blue" - and keep the same circle style with the white numbers. **
Leaflet has the concept of overlay groups (see here)
library(magrittr)
leaflet() %>%
addTiles() %>%
addCircleMarkers(label = ~type, data = map_data_1, group = "Map 1", color = "red") %>%
addCircleMarkers(label = ~type, data = map_data_2, group = "Map 2", color = "green") %>%
addCircleMarkers(label = ~type, data = map_data_3, group = "Map 3", color = "blue") %>%
addLayersControl(overlayGroups = c("Map 1", "Map 2", "Map 3"))