R语言 如何使用更详细(不同)的颜色制作热图以区分更接近的值?

r1zhe5dt  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(150)

代码:

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="")

情节

因为底部“行”中的值比其他值小得多,所以这些“其他值框”中的颜色都很暗,很难区分。我能做些什么来制作这样一个热图,即使某些行具有完全不同的值,也可以区分所有的值?

svgewumm

svgewumm1#

我最终使用了不同的功能。这是我的代码,如果其他人可能会感兴趣。
代码:

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"
        )
)

情节

相关问题