在 R Shiny Leaflet Maps 上创建动态图例,错误:“颜色”和“标签”的长度必须相同

问题描述 投票:0回答:1

我一直在努力在 R 闪亮的 Leaflet Maps 中创建动态地图图例。 我们的想法是拥有一个模板代码,以便每当数据发生变化时,地图及其图例都会随之变化。坐标已经能够随着数据的变化进行调整,但仍然不知道如何使图例也变得动态

这是代码

test_map.csv 包含数据列表

dat_map <- read.csv("data_input/test_map.csv", header = T)

这是针对用户界面的。其中有输入来访问 test_map.csv 中的特定数据,我将其命名为“querymap”

      tabItem(tabName = "maps",
          fluidRow(
             
             sidebarPanel(width = 4,
                          selectInput("querymap","Title:",dat_map$Title),
                          strong("Description:"), textOutput("captionmap"), br(),
                          strong("Reference:"), uiOutput("referencemap"), #textOutput("reference"), 
                          em(textOutput("latestmap")), br(),
                          strong("Tags: "), verbatimTextOutput("tagsmap")),
             mainPanel(
                h2(textOutput("dinTitlemap")),
                tabsetPanel(
                   tabPanel("Map",br(),leafletOutput("mapall")),
                   tabPanel("Table", br(), DT::dataTableOutput("dinTablemap")), 
                   tabPanel("Download", br(), downloadButton("dlTablemap", label = "Download Table"))
                )))))

这是从 test_map.csv 获取数据的“反应式”

    db_map <- reactive({
   filename <- dat_map$Filename[dat_map$Title == input$querymap]
   db_map <- read.csv(paste("data_input/", filename, sep = "")) %>% 
      select(-No)
   db_map})

这是输出代码

output$mapall <- renderLeaflet({
db_map <- db_map ()

  pal <- colorFactor(
     palette = c('red', 'blue', 'yellow', 'orange','grey','green'),
     domain = db_map$kategori) #pallete for coordinate and legend color
  
  addLegendCustom <- function(map, colors, labels, sizes, opacity = 0.8){
     colorAdditions <- paste0(colors, "; width:", sizes, "px; height:", sizes, "px")
     labelAdditions <- paste0("<div style='display: inline-block;height: ",
                              sizes, "px;margin-top: 4px;line-height: ",
                              sizes, "px;'>", labels, "</div>")
     
     return(addLegend(map, title = "Category", colors = colorAdditions,
                      labels = labelAdditions, opacity = opacity, position = "bottomleft"))}
  
  leaflet(options = leafletOptions(zoomControl = FALSE, minZoom = 3, maxZoom = 100), data = db_map) %>% 
     fitBounds(min(db_map$long),min(db_map$lat),max(db_map$long),max(db_map$lat)) %>%
     addTiles('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', group = "CartoDB Light") %>% 
     addCircleMarkers(
        radius = 7, 
        color = 'black',
        stroke = TRUE, weight = 1,
        fillOpacity = 0.7,
        fillColor = ~pal(kategori),
     )%>%
     kategori = as.factor(db_map$kategori) %>%
     addLegendCustom(colors = ~pal, labels = db_map$kategori, sizes = c(10,10,10)) %>% #here is the problem
     })

然后错误是“错误:'颜色'和'标签'必须具有相同的长度”

是的,我知道它的长度不一样,因为调色板设置为 6 种颜色,但标签取决于 db_map$kategori 中有多少个变量。所以我的问题是如何使用标签创建相同长度的颜色?

r shiny r-leaflet
1个回答
0
投票

您可以如下图所示进行操作。 如有必要,请向您的列表中添加更多颜色。

output$mapall <- renderLeaflet({
    db_map <- db_map()
    
    numcolor = length(unique(db_map$kategori))
    mycolor <- c("red", "blue", "black", "brown", "purple", "green", "darkblue", "darkgreen", "orange", "maroon", "yellow", "cyan","grey")
    pal <- mycolor[1:numcolor]  ##  this ensures that you have the same length of colors as labels
    
    # pal <- colorFactor(
    #   palette = c('red', 'blue', 'yellow', 'orange','grey','green'),
    #   domain = db_map$kategori) #pallete for coordinate and legend color
    
.
.
.
})
© www.soinside.com 2019 - 2024. All rights reserved.