3

How to join the two maps (m1 and m2) made in the leaflet? I created the two maps, but when I synchronize, the two maps are side by side (figure attached). However, I would like to join the two maps, that is, that the m2 points are in m1, leaving only one map. The executable code is below:

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

Thank you very much!

Antonio
  • 1,091
  • 7
  • 24

1 Answers1

3

Solution:

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 

Packages used:

# 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)
hello_friend
  • 5,682
  • 1
  • 11
  • 15
  • 1
    @Jose Edited please try the solution above and upvote and accept it if it does what you require ! – hello_friend May 06 '20 at 04:44
  • Hi @hello_friend, just a question, is you solution like superimposing layers? I mean, putting one layer (df1) over the first layer (example)? – Manu May 06 '20 at 12:52
  • @hello_friend, could you help me insert addLegend on my map, leaving the legend of the "example" database first, and then leaving the legend of the "df1" datbase below? Thanks again. – Antonio May 06 '20 at 14:48
  • @Jose Please post another question and I will answer it there ! – hello_friend May 06 '20 at 23:13
  • hello_friend, could you take a look please? https://stackoverflow.com/questions/61707326/features-for-my-shiny-code-separate-panel-filters-and-insert-popify-for-radiobu – Antonio May 10 '20 at 13:34
  • @Jose Hi mate, I will try and take a look at it tomorrow, hopefully you will have an answer by then but if not I will endeavour to meet the spec. – hello_friend May 10 '20 at 13:55
  • @ hello_friend: AMAZING answer! if you have time, could you please take a look at this question here? https://stackoverflow.com/questions/71197813/r-connecting-dots-on-a-map Thank you so much! – stats_noob Feb 23 '22 at 07:29