4
votes

I have a correlation heatmap produced in plotly. The scale scale goes from -1 to 1. As the correlation gets postively stronger, the tiles are coloured darker red. As the correlations gets negatively stronger, the tiles are coloured darker blue. I need zero values to be coloured white. However, the colourbar just chooses the colour of zero based on the distribution of the dataset. How can I force zero values to be white and in the middle of the colour bar? I tried using this answer, but can't get it to work for this heatmap. Please help I am going mad!

library(plotly)
library(magrittr)

# compute a correlation matrix
correlation <- round(cor(mtcars), 3)
nms <- names(mtcars)


colorlength <- 100

null_value <- (0 - min(correlation)) / (max(correlation) - min(correlation))        
border <- as.integer(null_value * colorlength)
colorscale <- as.list(1:colorlength)

#colorscale below zero
s <- scales::seq_gradient_pal("blue", "white", "Lab")(seq(0,1,length.out=border))
for (i in 1:border) {
  colorscale[[i]] <- c((i - 1) / colorlength, s[i])
}

#colorscale above zero
s <- scales::seq_gradient_pal("white", "red", "Lab")(seq(0,1,length.out=colorlength - border))
for (i in 1:(colorlength - border)) {
  colorscale[[i + border]] <- c((i + border) / colorlength, s[i])
}



plot_ly(x = nms, y = nms, z = correlation, 
            key = correlation, type = "heatmap", source = "heatplot",color = ~correlation,
            colorscale = colorscale,
            colorbar = list(len=1)) %>%
      layout(xaxis = list(title = ""), 
             yaxis = list(title = ""))

enter image description here

3

3 Answers

7
votes

It is pretty simple with ggplotly, but I think you can also do it with plotly. You could try this:

col3 <- colorRamp(c("red", "white", "blue"))

plot_ly(x = nms, y = nms, z = correlation, 
        key = correlation, type = "heatmap", source = "heatplot",color = col3,
        colorscale = colorscale,
        colorbar = list(len=1, limits = c(-1, 1))) %>%
    layout(xaxis = list(title = ""), 
           yaxis = list(title = ""))

enter image description here

2
votes

How about using ggplot and ggplotly:

library(tidyverse);
gg <- correlation %>%
    as.data.frame() %>%
    rownames_to_column("x") %>%
    gather(y, val, -x) %>%
    ggplot(aes(x, y, fill = val)) +
    geom_tile() +
    scale_fill_gradientn(
        colours = c("blue", "white", "red"),
        limits = c(-1, 1))

# Create plotly object
library(plotly);
ggplotly(gg);

enter image description here

Explanation: In ggplot we can use scale_fill_gradientn to set the limits explicitly, ensuring that white corresponds to a value of 0. ggplotly then converts the ggplot object to a plotly object.

1
votes

With plotly, using the graph_objects.Heatmap object, since you know the limits of the values of the heatmap you could just set zmax, zmid, zmin parameters to 1, 0, -1 respectively and the colorscale parameter to any colorscale with white in the middle, like RdBu.