如何加入传单中制作的两张 map (m1和m2)?我创建了两个 map ,但是当我同步时,两个 map 并排(附图)。但是,我想将两张图拼接起来,即m2点在m1中,只留一张图。可执行代码如下:
library(leaflet)
library(geosphere)
library(leafsync)
library(mapview)
#database
df<-structure(list(Properties = c(1,2,3,4,5,6,7,8,9,10), Latitude = c(-23.2, -23.6, -23.9, -23.9, -23.6, -23.5, -23.9, -23.9, -23.6, -23.9),
Longitude = c(-49.6, -49.6, -49.6, -49.4, -49.3, -49.9, -49.3, -49.2, -49.6, -49.9)), class="data.frame",row.names = c(NA, -10L))
df1<-structure(list(Properties = c(1,2,3,4,5), Latitude = c(-23.8, -23.4, -23.2, -23.7,-23.8),
Longitude = c(-49.9, -49.2, -49.3, -49.1,-49.9)), class="data.frame",row.names = c(NA, -3L))
#clusters
d<-as.dist(distm(df[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, 3)
df$cluster<-clusters
#Map using leaflet
example=df
getColor <- function(example) {
sapply(example$cluster, function(cluster) {
if(cluster == 1) {
"blue"
} else if(cluster == 2) {
"green"
} else if(cluster == 3) {
"orange"
} else {
"red"
} })
}
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = getColor(example)
)
m1=leaflet(example) %>% addTiles() %>%
addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(cluster))
m2=leaflet(df1) %>% addTiles() %>%
addAwesomeMarkers(lat=~Latitude, lng = ~Longitude)
sync(m1,m2)
非常感谢!
最佳答案
解决方案:
m3 = leaflet(example) %>% addTiles() %>%
addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(cluster)) %>%
addAwesomeMarkers(leaflet(df1) %>% addTiles(), lat=~df1$Latitude, lng = ~df1$Longitude)
m3
使用的包:
# Install pacakges if they are not already installed: necessary_packages => character vector
necessary_packages <- c("leaflet", "geosphere", "leafsync", "mapview", "dplyr")
# Create a vector containing the names of any packages needing installation:
# new_pacakges => character vector
new_packages <- necessary_packages[!(necessary_packages %in%
installed.packages()[, "Package"])]
# If the vector has more than 0 values, install the new pacakges
# (and their) associated dependencies: varied => stdout
if(length(new_packages) > 0){install.packages(new_packages, dependencies = TRUE)}
# Initialise the packages in the session: bool => stdout
lapply(necessary_packages, require, character.only = TRUE)
关于arrays - 加入 R 传单中制作的两张 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61627059/