R:一起加入地图
R: Joining Maps Together
我正在使用 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))
在这里 () 的这个问题中,我学会了如何“同步”这 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)
我的问题: 我不想将这 3 个地图“同步”- 我想制作一个包含所有 3 个地图作为“图层”的地图,这样您就可以在这 3 张地图之间“切换”。这看起来像这样:
我在这里 link 找到了这个,它展示了如何在传单地图中创建图层: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))
不幸的是,上面的代码创建了一个包含空图层的地图:
谁能告诉我如何将这三张地图(m1、m2、m3)直接组合成一张带有图层的地图?
谢谢!
** 注意:我想为所有地图保留完全相同的着色方案:第一个点为“红色”,所有其他点为“蓝色”- 并使用白色数字保持相同的圆形样式。谢谢! **
Leaflet 有叠加组的概念(参见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"))
我正在使用 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))
在这里 (
# 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)
我的问题: 我不想将这 3 个地图“同步”- 我想制作一个包含所有 3 个地图作为“图层”的地图,这样您就可以在这 3 张地图之间“切换”。这看起来像这样:
我在这里 link 找到了这个,它展示了如何在传单地图中创建图层: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))
不幸的是,上面的代码创建了一个包含空图层的地图:
谁能告诉我如何将这三张地图(m1、m2、m3)直接组合成一张带有图层的地图?
谢谢!
** 注意:我想为所有地图保留完全相同的着色方案:第一个点为“红色”,所有其他点为“蓝色”- 并使用白色数字保持相同的圆形样式。谢谢! **
Leaflet 有叠加组的概念(参见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"))