0

I created a map with the leaflet with two databases (df and df1). I managed to insert the legend of the database df, which are points referring to the clusters that I generated. However I would like to insert the database icon df1 in this same Legend. I inserted an attached image to illustrate. In the same legend, it shows the clusters, as well as the icon for the df1 database. The description of this icon I designated as "contact point". 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, -5L))

#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)
)

 ai_colors <-c("red","gray","blue","orange","green","beige","darkgreen","lightgreen", "lightred", "darkblue","lightblue",
      "purple","darkpurple","pink", "cadetblue","white","darkred", "lightgray","black")
  clust_colors <- ai_colors[df$cluster]
  icons <- awesomeIcons(
    icon = 'ios-close',
    iconColor = 'black',
    library = 'ion',
    markerColor =  clust_colors)


m = leaflet(example) %>% addTiles() %>%
  addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~as.character(cluster)) %>% 
  addAwesomeMarkers(leaflet(df1) %>% addTiles(), lat=~df1$Latitude, lng = ~df1$Longitude) %>% 
  addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = 
  unique(df$cluster))  
m 


enter image description here

Thank you very much!

3
  • Please see my edited solution below, and if it does what you are asking upvote and accept! Commented May 7, 2020 at 5:00
  • Thanks for reply. I really liked the one you did, it looks good, but in fact I will need to insert only one icon in the legend of the clusters. The answer I did is doing what I want, it just isn't putting the icon in the legend of the clusters together.
    – Antonio
    Commented May 7, 2020 at 13:28
  • Hello_friend, I inserted the image in the answer I made for you to see. The way I did it is right, only the caption of the icon I was unable to insert in the caption of the clusters.
    – Antonio
    Commented May 7, 2020 at 13:31

1 Answer 1

2

Here is a yucky very manual way but it gets the job done:

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, -5L))

#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

ai_colors <-c("red","gray","blue","orange","green","beige","darkgreen","lightgreen", "lightred", "darkblue","lightblue",
              "purple","darkpurple","pink", "cadetblue","white","darkred", "lightgray","black")
clust_colors <- ai_colors[df$cluster]
icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor =  clust_colors)

# Choose Icon:
propertiesIcons <- icons(
  iconUrl = ifelse(df1$Properties,
                   "http://icons.iconarchive.com/icons/icons-land/vista-map-markers/72/Map-Marker-Marker-Outside-Chartreuse-icon.png"
  ),
  iconWidth = 45, iconHeight = 40,
  iconAnchorX = 25, iconAnchorY = 12)


cluster1Icon <- icons( 
  "http://icons.iconarchive.com/icons/paomedia/small-n-flat/72/map-marker-icon.png",
  iconWidth = 45, iconHeight = 40,
  iconAnchorX = 25, iconAnchorY = 12)

cluster2Icon <- icons( 
  "http://icons.iconarchive.com/icons/icons-land/vista-map-markers/72/Map-Marker-Marker-Outside-Azure-icon.png",
  iconWidth = 45, iconHeight = 40,
  iconAnchorX = 25, iconAnchorY = 12)


cluster3Icon <- icons(
  "http://icons.iconarchive.com/icons/icons-land/vista-map-markers/72/Map-Marker-Marker-Inside-Chartreuse-icon.png",
  iconWidth = 45, iconHeight = 40,
  iconAnchorX = 25, iconAnchorY = 12)


clusterIcons <- list(cluster1Icon, cluster2Icon, cluster3Icon)

html_legend <- c("<img src='http://icons.iconarchive.com/icons/icons-land/vista-map-markers/72/Map-Marker-Marker-Outside-Chartreuse-icon.png'>green",
                 "<img src='http://icons.iconarchive.com/icons/paomedia/small-n-flat/72/map-marker-icon.png'>red",
                 "<img src='http://icons.iconarchive.com/icons/icons-land/vista-map-markers/72/Map-Marker-Marker-Outside-Azure-icon.png'>blue",
                 "<img src='http://icons.iconarchive.com/icons/icons-land/vista-map-markers/72/Map-Marker-Marker-Inside-Chartreuse-icon.png'>grey"
                 )


# Produce Map:

m <-
  leaflet(df1) %>%
  addTiles() %>%
  addMarkers(
    ~ Longitude,
    ~ Latitude,
    label = ~ as.character(df1$Properties),
    icon = propertiesIcons
  ) %>%
  addControl(html = html_legend, position = "topright") %>%
  addMarkers(
    lat =  ~ df$Latitude[which(df$cluster == "1")],
    lng = ~ df$Longitude[which(df$cluster == "1")],
    icon = clusterIcons[[1]],
    label = ~ as.character(df$cluster[which(df$cluster == "1")])
  ) %>%
  addMarkers(
    lat =  ~ df$Latitude[which(df$cluster == "2")],
    lng = ~ df$Longitude[which(df$cluster == "2")],
    icon = clusterIcons[[2]],
    label = ~ as.character(df$cluster[which(df$cluster == "2")])
  ) %>%
  addMarkers(
    lat =  ~ df$Latitude[which(df$cluster == "3")],
    lng = ~ df$Longitude[which(df$cluster == "3")],
    icon = clusterIcons[[3]],
    label = ~ as.character(df$cluster[which(df$cluster == "3")])
  )
m
5
  • @Jose Please see: stackoverflow.com/questions/37862467/… Commented May 7, 2020 at 2:17
  • Perfect. I will try, thanls hello_friend. If I can do it I will insert it here.
    – Antonio
    Commented May 7, 2020 at 2:30
  • @Jose no worries, inspect element on each marker to get the marker url. Commented May 7, 2020 at 2:31
  • Thanks friend. I got it, I just want to merge the two legends, can you help me? I left the code as an answer.
    – Antonio
    Commented May 7, 2020 at 3:05
  • @Jose please accept my solution if it does what you require. Commented May 8, 2020 at 0:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.