在小叶生成的地图图例中插入图标 [英] Insert icon in the legend of a map generated by leaflet
问题描述
我用带有两个数据库(df和df1)的传单创建了一个地图.我设法插入数据库df的图例,这些图例指向我生成的集群.但是,我想在同一图例中插入数据库图标df1.我插入了一个图片进行说明.在同一图例中,它显示了群集以及df1数据库的图标.我将此图标的描述指定为接触点".可执行代码如下
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
非常感谢!
推荐答案
这是一种非常讨厌的手动方法,但是可以完成工作:
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
这篇关于在小叶生成的地图图例中插入图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!