我有一个闪亮的代码,可以生成属性集群,如下所示,它显示在
leaflet
中制作的地图上。如果我将鼠标光标放在属性上,则会出现相应簇的编号,但我想要的是它显示它是哪个属性编号。例如,是1、2、3、4、5、6还是7? 你能帮我解决这个问题吗?
下面的可执行代码:
library(shiny)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)
function.cl<-function(df,k){
#database df
df<-structure(list(Properties = c(1,2,3,4,5,6,7),
Latitude = c(-23.8, -23.8, -23.9, -23.9, -23.9,-23.4,-23.5),
Longitude = c(-49.6, -49.3, -49.4, -49.8, -49.6,-49.4,-49.2),
Waste = c(526, 350, 526, 469, 285, 433, 456)), class = "data.frame", row.names = c(NA, -7L))
#clusters
coordinates<-df[c("Latitude","Longitude")]
d<-as.dist(distm(coordinates[,2:1]))
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average, k)
nclusters<-matrix(table(clusters))
df$cluster <- clusters
df1<-df[c("Latitude","Longitude")]
#Color and Icon for map
ai_colors <-c("red","gray","blue","orange","green","beige")
clust_colors <- ai_colors[df$cluster]
icons <- awesomeIcons(
icon = 'ios-close',
iconColor = 'black',
library = 'ion',
markerColor = clust_colors)
# Map for all clusters:
m1<-leaflet(df1) %>% addTiles() %>%
addMarkers(~Longitude, ~Latitude) %>%
addAwesomeMarkers(lat=~df$Latitude, lng = ~df$Longitude, icon=icons, label=~as.character(df$cluster)) %>%
addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))
plot1<-m1
return(list(
"Plot1" = plot1
))
}
ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Cl",
tabPanel("Solution",
sidebarLayout(
sidebarPanel(
tags$b(h3("Choose the cluster number?")),
sliderInput("Slider", h5(""),
min = 2, max = 5, value = 3),
),
mainPanel(
tabsetPanel(
tabPanel("Solution", (leafletOutput("Leaf1",width = "95%", height = "600")))))
))),
mainPanel(
tabsetPanel(
)))
server <- function(input, output, session) {
Modelcl<-reactive({
function.cl(df,input$Slider)
})
output$Leaf1 <- renderLeaflet({
Modelcl()[[1]]
})
}
shinyApp(ui = ui, server = server)
您所要做的就是将您的
leaflet
地图更改为:
m1 <- leaflet(df1) %>% addTiles() %>%
addMarkers(~ Longitude, ~ Latitude) %>%
addAwesomeMarkers(lat = ~ df$Latitude, lng = ~ df$Longitude, icon = icons,
label = ~ as.character(df$Properties)) %>%
addLegend(position = "topright",
title = "Cluster",
colors = ai_colors[1:max(df$cluster)],
labels = unique(df$cluster))
也就是说,您将
df$Properties
指定为 label
而不是 df$cluster
。