How to make a heatmap with more detailed (different) colors to distinguish closer values?

85 Views Asked by At

Code:

library("lattice")
n <- (1:10)*10
Dynamic_n <- (1:10)*10
data <- expand.grid(X=n, Y=Dynamic_n)
results <- c(0.74, 0.648, 0.544, 0.457, 0.406, 0.344, 0.327, 0.278, 
                       0.277, 0.232, 0.87, 0.913, 0.919, 0.898, 0.873, 0.839, 0.8, 0.754, 
                       0.703, 0.64, 0.889, 0.927, 0.952, 0.949, 0.93, 0.935, 0.918, 
                       0.9, 0.909, 0.875, 0.871, 0.925, 0.951, 0.958, 0.954, 0.959, 
                       0.958, 0.949, 0.933, 0.94, 0.876, 0.952, 0.944, 0.955, 0.973, 
                       0.965, 0.963, 0.964, 0.968, 0.971, 0.871, 0.932, 0.956, 0.961, 
                       0.955, 0.97, 0.964, 0.973, 0.971, 0.969, 0.887, 0.938, 0.958, 
                       0.974, 0.968, 0.959, 0.967, 0.974, 0.97, 0.966, 0.881, 0.944, 
                       0.961, 0.954, 0.966, 0.961, 0.958, 0.965, 0.979, 0.971, 0.878, 
                       0.927, 0.954, 0.966, 0.967, 0.953, 0.959, 0.973, 0.976, 0.969, 
                       0.868, 0.932, 0.952, 0.965, 0.964, 0.97, 0.964, 0.976, 0.966, 
                       0.974)
data$results <- results

levelplot(results ~ X*Y, data = data, xlab="X",
          main="")

Plot

enter image description here

Because the values in the bottom "row" is much smaller than the other values, the colors in those "other values boxes" are all quite dark and hard to distinguish. What can I do to make such a heatmap that all values can be distinguished albeit certain row has quite different values?

1

There are 1 best solutions below

0
J.Z. On

I end up using a different function. Here is my code in case others might be interested in it.

Code:

library(circlize)
library(ComplexHeatmap)

data <- structure(c(0.74, 0.648, 0.544, 0.457, 0.406, 0.344, 0.327, 0.278, 
                    0.277, 0.232, 0.87, 0.913, 0.919, 0.898, 0.873, 0.839, 0.8, 0.754, 
                    0.703, 0.64, 0.889, 0.927, 0.952, 0.949, 0.93, 0.935, 0.918, 
                    0.9, 0.909, 0.875, 0.871, 0.925, 0.951, 0.958, 0.954, 0.959, 
                    0.958, 0.949, 0.933, 0.94, 0.876, 0.952, 0.944, 0.955, 0.973, 
                    0.965, 0.963, 0.964, 0.968, 0.971, 0.871, 0.932, 0.956, 0.961, 
                    0.955, 0.97, 0.964, 0.973, 0.971, 0.969, 0.887, 0.938, 0.958, 
                    0.974, 0.968, 0.959, 0.967, 0.974, 0.97, 0.966, 0.881, 0.944, 
                    0.961, 0.954, 0.966, 0.961, 0.958, 0.965, 0.979, 0.971, 0.878, 
                    0.927, 0.954, 0.966, 0.967, 0.953, 0.959, 0.973, 0.976, 0.969, 
                    0.868, 0.932, 0.952, 0.965, 0.964, 0.97, 0.964, 0.976, 0.966, 
                    0.974), dim = c(10L, 10L), dimnames = list(c("10", "20", "30", 
                                                                 "40", "50", "60", "70", "80", "90", "100"), c("10", "20", "30", 
                                                                                                               "40", "50", "60", "70", "80", "90", "100")))

# Create a color palette with 100 color levels
my_palette <- colorRamp2(c(0.3, 0.7, 0.9, 0.95, 1), c("white", "red", "pink", "blue", "white"))

# Generate the heatmap
Heatmap(data, 
        col = my_palette, 
        cluster_rows = FALSE,     # Don't cluster rows
        cluster_columns = FALSE,  # Don't cluster columns
        column_names_rot = 0,    # Rotate column names 90 degrees
        row_names_rot = 0,        # Keep row names upright
        heatmap_legend_param = list(
          direction = "vertical", 
          title_position = "topleft",
          at = c(0.3, 0.7, 0.9, 0.95, 1), 
          labels = c("0.3", "0.7", "0.9", "0.95", "1"),
          title = "Key"
        )
)

Plot

enter image description here