Black Lives Matter. Please consider donating to Black Girls Code today.

Colorscale for heatmaps

In the documentation for heatmaps, it says to use colorscale= to control the colors, but that doesn’t seem to do anything for me. Instead, I’m using the option colors=, which works for the most part, but is giving me some weird issues.

I’m using colorRamp to make a custom scale, which I’m using for multiple different heatmaps, and yet they are using slightly different scales. My scale goes from red to white to blue, which is how it turns out for most of my plots, but in some of them it seems to skip the white in the middle and go red to purple to blue.

I think that maybe the colors are being set based on the data points themselves, rather than the range I specified using zmin and zmax. Is there any way around this? I want all my scales to be the same

Here’s some example code to illustrate my problem. In the first, second, and fourth rows of plots, where the data is pretty evenly distributed, you can see that the scales are fine. In the third row however, where it’s bimodal, the scale skips the white and orange, in the middle, going from blue to red via purple instead.

This isn’t a huge deal in this example case, since for the values that get plotted, they are still roughly the right color in comparison to the other charts. In my actual code, however, it’s causing a big problem for a chart with a lot of values around 0 and then some extremes on either end. The values in the middle are all coming out purple, which makes it a lot harder to pick out the extreme values that I’m actually interested in (as well as just being ugly).

ui.R

if (!require("pacman")) install.packages("pacman")

pacman::p_load(shiny, plotly)

nCodeNames <- c("a","b","c","d","e","f","g","h","i","j","k","l")


shinyUI(navbarPage(
  "E-N Matrics: Proportion of E-Code Resulting in each N-Code",
  
  tabPanel("Grid",

            lapply(c(1:4), function(i) fluidRow(
              lapply(c(1:3), function(j) column(4, plotlyOutput(paste0("grid",nCodeNames[(i-1)*3+j]))))
             ))
          
  )
  ))

server.R

if (!require("pacman")) install.packages("pacman")

pacman::p_load(shiny, plotly)

nCodeNames <- c("a","b","c","d","e","f","g","h","i","j","k","l")


shinyUI(navbarPage(
  "E-N Matrics: Proportion of E-Code Resulting in each N-Code",
  
  tabPanel("Grid",

            lapply(c(1:4), function(i) fluidRow(
              lapply(c(1:3), function(j) column(4, plotlyOutput(paste0("grid",nCodeNames[(i-1)*3+j]))))
             ))
          
  )
  ))