2

I have a problem plotting the colors of the legend using the viridis palette: the colors are not displayed although the legend labels are.

enter image description here

I tested the same code under Ubuntu with Shiny Server v1.4.2.786 with Node.js v0.10.40 (it doesn't display the viridis colors) and under MacOS (it does correctly).

The details of the Ubuntu R session:

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

This is the legend that doesn't display the colors

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

while this works also on the Ubuntu machine

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

enter image description here

It really seems to be a problem with the color codes of the viridis palette (I tried copying/pasting them in a character vector).

A working example

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)
CptNemo
  • 6,455
  • 16
  • 58
  • 107

3 Answers3

1

This has to do with the alpha channel #xxxxxxFF of the viridis palette. I had this very same issue when making viridis the default palette for the mapview package. I've written a little function to solve this. The function is not imported to the namespace, so you can access it only via mapview:::col2Hex. It is defined as:

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)

}

and the source can be found here.

This way, your code should work.

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

Try setting alpha to TRUE and you end up with no colors:

leaflet() %>% addTiles() %>% addLegend(
    position = 'bottomright',
    colors = mapview:::col2Hex(viridis(8), alpha = TRUE), 
    labels = mapview:::col2Hex(viridis(8), alpha = TRUE), opacity = 1)
TimSalabim
  • 5,604
  • 1
  • 25
  • 36
1

Development version of leaflet now supports viridis palettes. https://github.com/rstudio/leaflet/pull/364

Leni Ohnesorge
  • 716
  • 8
  • 19
0

I am running a Ubuntu machine 14.04LTS. I was able to get the colors on the legend but it looks like the colors are not listed in the colors() function and there for the legends labels are still hex code.

This part of code should retrieve the color names:

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

the modified app.R code

    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)

Let me know it this is helpful ...

Valter Beaković
  • 3,140
  • 2
  • 20
  • 30