将地图连接在一起

问题描述 投票:0回答:1

使用“leaflet”库,我制作了以下 3 张地图:

#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))

在这里的这个问题中(加入 R 中传单中制作的两张地图),我学会了如何“同步”这 3 张地图:

# 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)

enter image description here

我的问题: 这 3 个地图不是“同步”的 - 我想制作一张包含所有 3 个地图作为“图层”的地图,这样您就可以在这 3 个地图之间“切换”。这看起来像这样:

enter image description here

我在这里找到了这个链接,它展示了如何在传单地图中创建图层:https://poldham.github.io/abs/mapgbif.html

但这会为不同“类型”的点创建图层 - 而不是为不同的地图创建图层。我想我可以“调整”我的代码,将所有 3 个文件合并到一个文件中并相应地标记它们(使用新的标签类型变量):

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))

不幸的是,上面的代码创建了一个空图层的地图:

enter image description here

有人可以告诉我如何直接将这三个地图(m1,m2,m3)组合成一个带有图层的地图吗?

** 注意:我想为所有地图保留完全相同的着色方案:第一个点“红色”,所有其他点“蓝色” - 并与白色数字保持相同的圆圈样式。 **

r dplyr visualization geospatial r-leaflet
1个回答
2
投票

Leaflet 有叠加组的概念(参见此处

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"))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.