arrays - 加入 R 传单中制作的两张 map

标签 arrays leaflet

如何加入传单中制作的两张 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)

enter image description here

非常感谢!

最佳答案

解决方案:

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/

相关文章:

java - 数组中的空单元格和非空单元格

c++ - C 字符串数组,用字符串文字初始化

leaflet - 一次 setBounds 到两个不同的 featureGroups mapbox 传单?

javascript - Leaflet .addLayer() 功能失败,在控制台工作

javascript - 如何在Leaflet.js中动态设置阴影

javascript - Leaflet JS - 居中和缩放功能组

r - 在传单 map 上注释数字变量

c++ - 对数组C++中的最大和最小值进行排序

javascript - 从 Javascript 数组对象中删除记录

javascript - 取消分割两个数组 Javascript