Home > Back-end >  Viridis - negative values (correlations) too similar, how color incrementally (any palette R code)?
Viridis - negative values (correlations) too similar, how color incrementally (any palette R code)?

Time:07-28

How might I use a palette in a gt table (probably in data_color() so that negative correlations are colored more incrementally using the color-blind safe palette viridis?

Negative correlations are often in too similar of a color in this table, to my eyes.

require(corrr)
require(gt)
require(viridis)
mtcars %>% 
  corrr::correlate() %>% 
  slice(1:10) %>% 
  gt() %>% 
  data_color(
    columns = where(is.numeric),
    colors = scales::col_numeric(
      ## option D for Viridis - correlation coloring
      palette = viridis(20, begin = 0, end = 1,
                        direction = 1, option ="D"), 
      domain = NULL)
  )

mtcars correlogram shows all negative values to be the same color using viridis, not desired

CodePudding user response:

Try direction = -1, option ="H"

CodePudding user response:

@redowl inspired experimenting with the viridis arguments. Here is what I have so far. I used viridis(option = "E") for the cividis color scale, autotext = FALSE to avoid distracting color text changes, reverse = 1 and used font size and bold to make the text more readable regardless of color.

enter image description here

require(corrr)
require(gt)
require(viridis)
require(RColorBrewer)

# Get a palette of 8 pastel colors from
# the RColorBrewer package
pal <- RColorBrewer::brewer.pal(5, "RdBu")

mtcars %>% 
  corrr::correlate() %>% 
  slice(1:10) %>% 
  gt() %>% 
  #Apply new style to all column headers
  tab_style(
    locations = cells_column_labels(columns = everything()),
    style     = list(
      # Give a thick border below
      cell_borders(sides = "bottom", weight = px(6)),
      # Make text bold
      cell_text(weight = "bold", size = "xx-large")
    )
  ) %>%
  # Larger body size
  tab_style(
    locations = cells_body(columns = where(is.numeric)),
    style     = list(
      #Make text bold
      cell_text(size = "x-large", color = "white")
    )
  ) %>% 
  # Larger column 1 size - match column headers
  tab_style(
    locations = cells_body(columns = 1),
    style     = list(
      #Make text bold
      cell_text(weight = "bold", size = "xx-large", color = "black")
    )
  ) %>% 
  data_color(
    columns = where(is.numeric),
    colors = scales::col_numeric(
      ## option E for cividis - correlation coloring
      palette = viridis(20, begin = 0, end = 1,
                        direction = -1, option ="E"), 
      domain = c(-1,1)),
    # Default value for auto_color_text is TRUE,
    #  distracting text color change
    autocolor_text = FALSE
  )
  • Related