【问题标题】:Shiny Server: leafet doesn't display viridis colors in legend闪亮的服务器:传单在图例中不显示绿色颜色
【发布时间】:2016-10-16 06:11:23
【问题描述】:

我在使用 viridis 调色板绘制图例颜色时遇到问题:虽然图例标签显示了颜色,但未显示颜色。

我在 Ubuntu 下使用 Shiny Server v1.4.2.786Node.js v0.10.40(它不显示 viridis 颜色)和在 MacOS 下(它正确)测试了相同的代码。

Ubuntu R 会话的详细信息:

R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 15.10

leaflet_1.0.1 shiny_0.13.2  viridis_0.3.4

这是不显示颜色的图例

    leaflet() %>% addTiles() %>% addLegend(
      position = 'bottomright',
      colors = viridis(8), 
      labels = viridis(8), opacity = 1)

虽然这也适用于 Ubuntu 机器

    leaflet() %>% addTiles() %>% addLegend(
      position = 'bottomright',
      colors = rgb(t(col2rgb(palette())) / 255), 
      labels = palette(), opacity = 1)

这似乎确实是 viridis 调色板的颜色代码有问题(我尝试将它们复制/粘贴到字符向量中)。

一个工作示例

library(shiny)
library(leaflet)
library(viridis)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

ui <- fluidPage(
  leafletOutput("mymap")
)

server <- function(input, output, session) {

  output$mymap <- renderLeaflet({
    leaflet() %>% addTiles() %>% addLegend(
      position = 'bottomright',
      colors = viridis(8), 
      labels = viridis(8), opacity = 1)

  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r ubuntu shiny leaflet viridis


    【解决方案1】:

    这与viridis 调色板的alpha 通道#xxxxxxFF 有关。在将 viridis 设为 ma​​pview 包的默认调色板时,我遇到了同样的问题。我写了一个小函数来解决这个问题。该函数未导入命名空间,因此您只能通过mapview:::col2Hex 访问它。定义为:

    function(col, alpha = FALSE) {
    
      mat <- grDevices::col2rgb(col, alpha = TRUE)
      if (alpha) {
        hx <- grDevices::rgb(mat[1, ]/255, mat[2, ]/255,
                             mat[3, ]/255, mat[4, ]/255)
      } else {
        hx <- grDevices::rgb(mat[1, ]/255, mat[2, ]/255, mat[3, ]/255)
      }
      return(hx)
    
    }
    

    来源可以在here找到。

    这样,您的代码应该可以工作。

    leaflet() %>% addTiles() %>% addLegend(
        position = 'bottomright',
        colors = mapview:::col2Hex(viridis(8)), 
        labels = mapview:::col2Hex(viridis(8)), opacity = 1)
    

    尝试将 alpha 设置为 TRUE,最终您将没有颜色:

    leaflet() %>% addTiles() %>% addLegend(
        position = 'bottomright',
        colors = mapview:::col2Hex(viridis(8), alpha = TRUE), 
        labels = mapview:::col2Hex(viridis(8), alpha = TRUE), opacity = 1)
    

    【讨论】:

      【解决方案2】:

      开发版的传单现在支持 viridis 调色板。 https://github.com/rstudio/leaflet/pull/364

      【讨论】:

        【解决方案3】:

        我正在运行 Ubuntu 机器 14.04LTS。我能够获得图例上的颜色,但看起来颜色未在 colors() 函数中列出,并且图例标签仍然是十六进制代码。

        这部分代码应该检索颜色名称:

        colors()[match(rgb(t(col2rgb(leafletColors)), 
                               maxColorValue = 255), c(rgb(t(col2rgb(colors())), maxColorValue = 255)))]
        

        修改后的app.R代码

            library(shiny)
            library(leaflet)
            library(viridis)
        
            r_colors <- rgb(t(col2rgb(colors()) / 255))
            names(r_colors) <- colors()
            leafletColors <- palette(viridis(8))
        
            ui <- fluidPage(
                    leafletOutput("mymap"),
                    p(),
                    actionButton("recalc", "New points")
            )
        
            server <- function(input, output, session) {
        
                    points <- eventReactive(input$recalc, {
                            cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
                    }, ignoreNULL = FALSE)
        
                    output$mymap <- renderLeaflet({
                            leaflet() %>%
                                    addProviderTiles("Stamen.TonerLite",
                                                     options = providerTileOptions(noWrap = TRUE)
                                    ) %>%
                                    addMarkers(data = points()) %>% 
                                    addLegend(
                                            position = 'bottomright',
                                            colors = leafletColors, 
                                            labels = palette(), opacity = 1)
                    })
            }
        
            shinyApp(ui, server)
        

        让我知道这很有帮助...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-21
          • 1970-01-01
          • 2015-10-11
          • 1970-01-01
          • 2014-08-23
          相关资源
          最近更新 更多