Вставить значок в легенду карты, созданной листовкой - PullRequest
1 голос
/ 07 мая 2020

Я создал карту с листовкой с двумя базами данных (df и df1). Мне удалось вставить легенду базы данных df, которая представляет собой точки, относящиеся к кластерам, которые я сгенерировал. Однако я хотел бы вставить значок базы данных df1 в эту же легенду. Я вставил прикрепленное изображение для иллюстрации. В той же легенде показаны кластеры, а также значок базы данных df1. Описание этого значка я обозначил как «точка контакта». Исполняемый код ниже

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

Большое спасибо!

1 Ответ

2 голосов
/ 07 мая 2020

Вот неприятный, очень ручной способ, но он выполняет свою работу:

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
...