R ComplexHeatmap-高亮显示热图中的特定值

R ComplexHeatmap-高亮显示热图中的特定值,r,heatmap,R,Heatmap,我正在使用R中的ComplexHeatmap包绘制一个矩阵。 我想用不同的颜色绘制具有特定值的单元格,例如: 如果值

我正在使用R中的ComplexHeatmap包绘制一个矩阵。 我想用不同的颜色绘制具有特定值的单元格,例如:

  • 如果值<阈值低,则为红色
  • 如果阈值低<值<阈值高,则为黄色
  • 如果阈值_高<值<0.9,绿色梯度
  • 如果值>0.9,则为白色
下面的代码产生了一些接近我试图实现的东西,但不完全是

  • 应以“红色”突出显示3个值(仅显示)
  • 图例不显示红色值
我在colorRamp2()中尝试了不同的值,但仍然达到了我想要的结果

链接到程序包:

相关文件:

可复制示例:

library(ComplexHeatmap)
library(circlize)
library(RColorBrewer)

mat <- structure(c(1, 0.154616894313456, 0.168336307012768, 1, 1, 0.453975613676526, 
            1, 1, 1, 1, 0.00056792615275985, 1, 1, 1, 1, 1, 0.105149642433548, 
            0.425158360263792, 1, 1, 0.501057722133202, 1, 1, 1, 0.00278294210196398, 
            1, 0.168271031634512, 1, 0.830063213792425, 1, 1, 1, 1, 1, 1, 
            1, 1, 1, 0.451156419955746, 1, 0.0274775797365075, 1, 1, 0.717017044202449, 
            1, 1, 1, 0.717321290931695, 0.0845704812428829, 1, 0.0699956751028108, 
            1, 0.275329648641519, 1, 0.0201980178890521, 0.0455613967501329, 
            1, 1, 0.536035636664544, 1, 1, 1, 1, 0.358910397894989, 1, 1, 
            1, 1, 1, 0.0467579359404697, 0.100390014456881, 1, 1, 1, 1, 1, 
            1, 1, 1, 1, 1, 0.00196501415273741, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
            0.52718575076658, 0.589648919728843, 0.016642521684359, 1, 1, 
            0.100523837174713, 0.62858401844093, 1, 1, 1, 0.00272095544860866, 
            1, 0.00152884219028376, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.00715615768151061, 
            1, 1, 0.0017044025565658, 1, 0.900838683850751, 1, 0.00286177891699905, 
            0.964009307250068, 1, 1, 1, 1, 0.712505373834541, 0.156595117903911, 
            0.698630689400136, 0.0497797620046933, 0.0432669236844255, 1, 
            0.482930753871518, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.863198034258964, 
            0.0914794415922655, 1, 1, 1, 1, 1, 0.130770966476073, 0.133939889164611, 
            1, 0.357244624441367, 0.0574932940137384, 1, 1, 1, 1, 1, 1, 1, 
            1, 1, 1, 1, 1, 1, 1, 1, 0.10152332525958, 1, 1, 1, 1, 1, 1, 1, 
            0.613891861387759, 1, 1, 1, 1, 0.0194347260355869, 1, 1, 1, 1, 
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.836989723814452, 
            0.262496570009738, 0.0266926025635128, 1, 1, 1, 0.409918984740791, 
            1, 1, 1, 0.000826603369076878, 0.251385649690035, 0.262496570009738, 
            1, 0.636285376736921, 1, 1, 1, 0.131975914025068, 1, 0.302372382987271, 
            1, 0.829657680365184, 0.234804722298138, 1, 0.480189087472427, 
            1, 1, 0.434351639718704, 1, 1, 1, 1, 1, 1, 1, 1, 0.827162805570832, 
            1, 0.000754621951469572, 1, 1, 1, 0.0607557227589793, 0.0466844602596043, 
            0.166584131048057, 1, 0.00375850252234914, 1, 0.00714919767503871, 
            1, 1, 0.0274923567061487, 0.317749204479313, 0.523992490899291, 
            0.0154308196673378, 1, 1, 1, 1, 0.283874713710679, 1, 1, 0.0879611832530085, 
            0.00614031465663928, 1, 0.44858333972042, 1, 1, 1, 1, 1, 1, 0.0999740979976858, 
            1, 0.0857648171802508, 0.129753363733359, 0.205913325652849, 
            0.164132124317898, 1, 1, 0.135339969223928, 0.441555384681617, 
            1, 1, 1, 0.619544298689775, 1, 1, 1, 1, 1, 0.00237701375005596, 
            1, 1, 0.149979241487463, 5.65028970245137e-06, 0.00435404367430347, 
            0.102437190438328, 0.159652054514079, 1.34807420832168e-05, 0.0781174367417338, 
            0.00168329325806749, 1, 1, 0.0178846842165573, 0.562000511433683, 
            1, 0.226033494133538, 1, 1, 1, 0.0116432115170938, 0.0158685033438527, 
            1, 1, 1, 1, 1, 0.226315459031352, 1, 0.0839844171448521, 0.00448638209236464, 
            0.741937003903222, 1, 1, 0.0969877781197935, 1, 1, 0.899533885687393, 
            0.11038695014775, 0.0691406897023532, 0.00864645807981477, 0.000409055625639548, 
            0.0432931123545735, 0.000183686382465392, 1, 1, 1, 1, 1, 0.262742531844371, 
            1, 0.695486348184785, 0.129359897480463, 0.7456928940893, 0.85934081881042, 
            1, 1, 1, 1, 1, 0.608552296219399, 1, 0.39067450494575, 1, 1, 
            1, 1, 0.411281105041672, 0.484196212791728, 0.198292538014945, 
            0.677053068881975, 1, 1, 1, 0.0318317218601013, 1, 1, 0.791347998335414, 
            1, 1, 1, 1, 0.240600397196919, 0.135644227364626, 1, 1, 1, 8.78013264925257e-05, 
            0.259727834661945, 0.305025003277675, 0.0492101609648877, 1, 
            0.667608208227447, 0.15068985721195, 1, 0.00197605148651341, 
            0.194624037083573, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.0758606965055501, 
            1, 1, 1, 1, 0.146826771370067, 1, 0.0318317218601013, 1, 0.267193726901327, 
            1, 0.0214353975572249, 1, 1), .Dim = c(53L, 8L), .Dimnames = list( NULL, NULL))

threshold_low <- 0.00015
threshold_high <- 0.05
color_breaks <-c(0, threshold_low, threshold_high, 0.9)
my_palette <- c( "red",
                 "yellow", 
                 colorRampPalette(rev(brewer.pal(8, "Greens")))(n = length(color_breaks)-3),
                 "white")
col_fun = colorRamp2(color_breaks, my_palette)

Heatmap(mat,
        cluster_rows = FALSE,
        show_column_names = FALSE, # We use colored anno_block instead
        #clustering_distance_rows = "euclidean",
        cluster_columns=FALSE,

        col = col_fun, 
        # Legend
        heatmap_legend_param = list(
          title = "values",  direction = "horizontal",
          legend_width = unit(.25, "npc"),
          at = c(threshold_low, threshold_high, .5), 
          labels = c("<0.00015" , "<0.05", '1')
        ),
        # Cell separator
        rect_gp = gpar(col = "lightgray", lwd = 1),
        width=unit(0.6,'npc'),
)
库(ComplexHeatmap)
图书馆(圆形)
图书馆(RColorBrewer)

mat以下是我创建的颜色映射函数:

offset = 1e-10
col_fun = colorRamp2(c(0, 0.00015, 0.00015+offset, 0.05, 0.05+offset, 0.9, 0.9+offset, 1),
                     c("red", "red", "yellow", "yellow", "lightgreen", "darkgreen", "white", "white"))

信件是

0                 "red"
0.00015           "red"
0.00015+offset    "yellow"
0.05              "yellow"
0.05+offset       "lightgreen"
0.9               "darkgreen"
0.9+offset        "white"
1                 "white"
问题是在不同的时间间隔内有不同的颜色模式,需要注意每个时间间隔的端点