2016-10-16 9 views
2

凡例の色をプロットする際に、viridisパレットを使用すると問題が発生します。凡例のラベルは表示されません。シャイニーサーバ:凡例にヴィリチスの色が表示されない

enter image description here

私は(それは正しくありません)Node.js v0.10.40と(それがビリディス色を表示しない)とMacOSの下Shiny Server v1.4.2.786とUbuntuの下に同じコードをテストしました。

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 

これは、これはUbuntuマシンでも

leaflet() %>% addTiles() %>% addLegend(
     position = 'bottomright', 
     colors = rgb(t(col2rgb(palette()))/255), 
     labels = palette(), opacity = 1) 
を働きながら色

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

は表示されません伝説です

enter image description here

実際には、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) 

答えて

0

私はUbuntuマシン14.04LTSを実行しています。私は凡例に色を得ることができましたが、色がcolors()関数にリストされていないように見え、凡例のラベルはまだ16進コードです。

コードのこの部分は、色の名前取り出す必要があります。私は、これは便利です、それを知ってみましょう

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) 

を...

1

これは持っていますviridisパレットのアルファチャンネル#xxxxxxFFと関係があります。 mapviewパッケージのデフォルトパレットをviridisにすると、これと同じ問題が発生しました。私はこれを解決するために少し機能を書いた。この関数は名前空間にインポートされないため、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) 

TRUEにアルファを設定してみてください、あなたは無色で終わる:リーフレットの

leaflet() %>% addTiles() %>% addLegend(
    position = 'bottomright', 
    colors = mapview:::col2Hex(viridis(8), alpha = TRUE), 
    labels = mapview:::col2Hex(viridis(8), alpha = TRUE), opacity = 1) 
関連する問題