R 有光泽的单张制作的地图中的颜色问题

R 有光泽的单张制作的地图中的颜色问题,r,leaflet,R,Leaflet,朋友们,你能帮我回答以下问题吗 我对使用传单制作的第二张地图的颜色有点问题。无论我选择哪个簇,簇的颜色都会变为蓝色,但是我希望颜色与第一张地图上显示的相应簇的颜色相同。例如,簇1在第一个贴图上是红色的,但是当我选择簇1生成第二个贴图时,它显示为蓝色,而不是红色。我在下面插入了一张图片来说明。感谢您的帮助。可执行代码如下所示 library(shiny) library(ggplot2) library(rdist) library(geosphere) library(shinythemes)

朋友们,你能帮我回答以下问题吗

我对使用传单制作的第二张地图的颜色有点问题。无论我选择哪个簇,簇的颜色都会变为蓝色,但是我希望颜色与第一张地图上显示的相应簇的颜色相同。例如,簇1在第一个贴图上是红色的,但是当我选择簇1生成第二个贴图时,它显示为蓝色,而不是红色。我在下面插入了一张图片来说明。感谢您的帮助。可执行代码如下所示

library(shiny)
library(ggplot2)
library(rdist)
library(geosphere)
library(shinythemes)
library(leaflet)

function.cl<-function(df,k,Filter1,Filter2){
  
  #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 
  
  #specific cluster and specific propertie
  df1<-df[c("Latitude","Longitude")]
  df1$cluster<-as.factor(clusters)
  df_spec_clust <- df[df$cluster == Filter1,]
  df_spec_prop<-df1[df$Properties==Filter2,]
  
  #Table to join df and df1
  data_table <- Reduce(merge, list(df, df1))
  
  #Color and Icon for map
  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)
  
  leafIcons <- icons(
    iconUrl = ifelse(df1$Properties,
                     "https://image.flaticon.com/icons/svg/542/542461.svg"
    ),
    iconWidth = 45, iconHeight = 40,
    iconAnchorX = 25, iconAnchorY = 12)
  html_legend <- "<img src='https://image.flaticon.com/icons/svg/542/542461.svg'>"
  
  # Map for all clusters:
  m1<-leaflet(df1) %>% addTiles() %>%
    addMarkers(~Longitude, ~Latitude, icon = leafIcons) %>%
    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
  
  # Map for specific cluster and propertie
  m2<-leaflet(df_spec_clust) %>% addTiles() %>%
    addMarkers(~Longitude, ~Latitude, icon = leafIcons) %>%
    addAwesomeMarkers(lat=~df_spec_prop$Latitude, lng = ~df_spec_prop$Longitude, icon=icons, label=~as.character(df$cluster)) 
    plot2<-m2
  
  
  return(list(
    "Plot1" = plot1,
    "Plot2" = plot2,
    "Data" = data_table
  ))
}

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")))))
                        
                      ))),
  tabPanel("",
           sidebarLayout(
             sidebarPanel(
               selectInput("Filter1", label = h4("Select just one cluster to show"),""),
               selectInput("Filter2",label=h4("Select the cluster property designated above"),""),
             ),
             mainPanel(
               tabsetPanel(
                 tabPanel("Map", (leafletOutput("Leaf2",width = "95%", height = "600")))))
           )))

server <- function(input, output, session) {
  
  Modelcl<-reactive({
    function.cl(df,input$Slider,input$Filter1,input$Filter2)
  })
  
  output$Leaf1 <- renderLeaflet({
    Modelcl()[[1]]
  })
  
  output$Leaf2 <- renderLeaflet({
    Modelcl()[[2]]
  })
  
  observeEvent(input$Slider, {
    abc <- req(Modelcl()$Data)
    updateSelectInput(session,'Filter1',
                      choices=sort(unique(abc$cluster)))
  }) 
  
  observeEvent(input$Filter1,{
    abc <- req(Modelcl()$Data) %>% filter(cluster == as.numeric(input$Filter1))
    updateSelectInput(session,'Filter2',
                      choices=sort(unique(abc$Properties)))
  }) 
  
  
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(GG2)
图书馆(rdist)
图书馆(地球圈)
图书馆(shinythemes)
图书馆(单张)

function.cl您的整个代码都需要重新访问。看看我是如何用
~Latitude
替换了
df1$Latitude
。通常最佳做法是不引用其他数据帧(仅使用传递给
传单()
的数据帧)

作为一个快速(肮脏)修复,您可以构建第二组图标:

  # Map for all clusters:
  m1<-leaflet(df1) %>% addTiles() %>%
    addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster) %>% 
    addLegend( position = "topright", title="Cluster", colors = ai_colors[1:max(df$cluster)],labels = unique(df$cluster))

  plot1<-m1
   # Map for specific cluster and propertie
  if(nrow(df_spec_clust)>0){
    clust_colors <- ai_colors[df_spec_clust$cluster]
    icons <- awesomeIcons(
      icon = 'ios-close',
      iconColor = 'black',
      library = 'ion',
      markerColor =  clust_colors)
    m2<-leaflet(df_spec_clust) %>% addTiles() %>%
    addAwesomeMarkers(lat=~Latitude, lng = ~Longitude, icon=icons, label=~cluster) 
  plot2<-m2} else plot2 <- NULL
#所有集群的映射:
m1%addTiles()%%>%
addAwesomeMarkers(纬度=~纬度,lng=~经度,图标=图标,标签=~簇)%>%
addLegend(position=“topright”,title=“Cluster”,colors=ai_colors[1:max(df$Cluster)],labels=unique(df$Cluster))
图10){
花色