将颜色映射到热图中的值(pheatmap)

时间:2014-02-05 17:46:01

标签: r heatmap pheatmap

一个简单的问题,虽然我无法弄清楚如何解决它。我有一个数据框,我使用热图通过pheatmap绘制了它的值。此数据框中的值范围为0.098到2.94。我的数据有 1s 的lof。我想将1设置为白色,其余值应为灰度,最大值为黑色。我得到了与我的代码相似的东西,但是希望的结果是包含从0.098到1的值的灰度级应该按照从浅灰色到深灰色的递增顺序递增。我不知道是否有可能。

非常感谢

我的代码:

bk = unique(c(seq(-2,0.98, length=50), seq(0.98,1, 50), seq(1, 4, length=50))) #to set 1 as white

colors = colorRampPalette(c("gray", "white", "black"))(length(bk)-1)

hm <- pheatmap(as.matrix(df), color=colors, breaks=bk, scale="none", cluster_rows=F, cluster_cols=F)

这是我的数据框的dput

structure(list(NA1 = c(1, 0.676970665403866, 0.180735940449527, 1), NA14 = c(1, 2.47335986105386, 1, 0.212477914884324), N15 = c(1, 2.01728907209032, 1, 1), NA21 = c(0.0984152452957471, 1.03793278974722, 0.164466463565078, 1), NA23 = c(0.198799370310219, 0.11781214850331, 1, 1), NA27 = c(1, 0.424548617359722, 1, 1), Normal_A8 = c(1, 2.09520458406257, 0.757835484114982, 0.157189452261399), N07 = c(1, 1.82165624404929, 1, 0.750571089868513), Normal_nappa08 = c(0.197195537223992, 1.2943605496238, 1, 0.794285534644797), N1 = c(1.25697210168845, 0.834233608595822, 1, 1.34137057159538), N19 = c(1, 1, 1, 0.860574607416193), N21 = c(0.113712044593094, 0.557227424763336, 1, 0.930914683241786), N25 = c(0.507590735428859, 1, 1, 0.800048340098232), N21 = c(1, 0.315218478784923, 1, 1), N6 = c(1, 1, 1, 1), NA17 = c(1, 1, 1, 1), NA27 = c(1, 1, 1, 1), NA8 = c(0.558064364395163, 1.10333648844397, 1, 1), OA10 = c(1, 1, 1, 1), OA12 = c(1, 1.83681188388072, 1.01642919085048, 0.533410476703701), OA13 = c(1, 1.132050506061, 1, 1), OA16 = c(1, 1, 1, 1), OA18 = c(1, 2.83528162888438, 2.35542585752491, 1.29415196470609), OA22 = c(1, 1.13005418598598, 1, 1), OA26 = c(2.0173633027974, 2.87949980947019, 1.58383227721729, 1.06280472739899), OA5 = c(1, 2.27572364708314, 1, 1), OA7 = c(1, 1.9536510191507, 1, 1), O05 = c(1.37192500010228, 2.40797118258484, 1, 1.61682421728384), O10 = c(0.464932731013085, 1.77383521716872, 1, 2.12342767372253), O13 = c(1, 2.94005281829921, 1, 1.20578447782737), O20 = c(1.24926832014166, 1.74277194088959, 1, 1.79755040142869), O23 = c(1, 1.2123413008167, 1, 1.02707435049319), O9 = c(1, 1.97284676801732, 1, 1.15270850832198), OA1 = c(1.81809297607138, 1.10632758180511, 0.761078669190445, 1), OA23 = c(1, 1, 1, 1), OA_slide_A9 = c(1, 0.324430405028968, 1, 1), O12 = c(0.773389048380233, 1, 1.24407185437909, 1), O5 = c(1, 1, 1, 1)), .Names = c("NA1", "NA14", "NA15", "NA21", "NA23", "NA27", "NA8", "N07", "N08", "N1", "N19", "N21", "N25", "N21", "N6", "NA17", "NA27", "NA8", "OA10", "OA12", "OA13", "OA16", "OA18", "OA22", "OA26", "OA5", "OA7", "O05", "O10", "O13", "O20", "O23", "O9", "OA1", "OA23", "OA9", "O12", "O5"), row.names = c("CD", "CHST", "LEP", "PCO"), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

我已经想到了怎么做,虽然我不知道这是不是最好的方法

#create the breaks
bk2 = unique(c(seq(0, 0.98, length=9), 1, seq(1.01,3.5, length=10)))

#set different color vectors for each interval
col1 = colorRampPalette(c("gray87", 'gray32'))(9) #set the order of greys
col2 <- rep("white", 2)
col3 = colorRampPalette(c("gray31", "gray0"))(9)
colors2 <- c(col1, col2, col3)


#draw heatmap
hm2 <- pheatmap(as.matrix(mat), color=colors2, breaks=bk2, scale="none", cluster_rows=F, cluster_cols=F)