代码之家  ›  专栏  ›  技术社区  ›  stats_noob

R:将地图连接在一起

  •  0
  • stats_noob  · 技术社区  · 3 年前

    我正在使用R编程语言。

    使用“传单”库,我制作了以下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))
    

    在这个问题上( Join two maps made in the leaflet in 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

    谢谢

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

    1 回复  |  直到 3 年前
        1
  •  2
  •   danlooo    3 年前

    传单有重叠组的概念(参见 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"))
    

    enter image description here

        2
  •  0
  •   stats_noob    3 年前

    @丹洛:非常感谢你的帮助!你能告诉我你的回答是否正确吗?

    leaflet() %>%
      addTiles() %>%
     addCircleMarkers(data = map_data_1, group = "Map 1", 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))  %>%
    addCircleMarkers(data = map_data_2, group = "Map 2", 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)) %>%
    addCircleMarkers(data = map_data_3, group = "Map 3", 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)) %>%
      addLayersControl(overlayGroups = c("Map 1", "Map 2", "Map 3"))
    

    enter image description here

    非常感谢你的帮助!