在R中为pheatmap设置0点

时间:2015-07-28 13:36:00

标签: r pheatmap

如何在此热图中将色标上的0点设置为白色?它是否使用breaks参数?

在以下代码中,white设置为3(或在比例附近):

test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Name", 1:20, sep = "")
pheatmap(test, color = colorRampPalette(c("yellow", "white", "blue"))(50))

enter image description here

2 个答案:

答案 0 :(得分:11)

这可以使用breaks来解决。中断对应于调色板的数字范围。

test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Name", 1:20, sep = "")

paletteLength <- 50
myColor <- colorRampPalette(c("yellow", "white", "blue"))(paletteLength)
# length(breaks) == length(paletteLength) + 1
# use floor and ceiling to deal with even/odd length pallettelengths
myBreaks <- c(seq(min(test), 0, length.out=ceiling(paletteLength/2) + 1), 
              seq(max(test)/paletteLength, max(test), length.out=floor(paletteLength/2)))

# Plot the heatmap
pheatmap(test, color=myColor, breaks=myBreaks)

要查看这些载体的内容:

> myColor
 [1] "#FFFF00" "#FFFF0A" "#FFFF14" "#FFFF1F" "#FFFF29" "#FFFF34" "#FFFF3E" "#FFFF48" "#FFFF53" "#FFFF5D"
[11] "#FFFF68" "#FFFF72" "#FFFF7C" "#FFFF87" "#FFFF91" "#FFFF9C" "#FFFFA6" "#FFFFB0" "#FFFFBB" "#FFFFC5"
[21] "#FFFFD0" "#FFFFDA" "#FFFFE4" "#FFFFEF" "#FFFFF9" "#F9F9FF" "#EFEFFF" "#E4E4FF" "#DADAFF" "#D0D0FF"
[31] "#C5C5FF" "#BBBBFF" "#B0B0FF" "#A6A6FF" "#9C9CFF" "#9191FF" "#8787FF" "#7C7CFF" "#7272FF" "#6868FF"
[41] "#5D5DFF" "#5353FF" "#4848FF" "#3E3EFF" "#3434FF" "#2929FF" "#1F1FFF" "#1414FF" "#0A0AFF" "#0000FF"
> myBreaks
 [1] -1.73042462 -1.66120764 -1.59199065 -1.52277367 -1.45355668 -1.38433970 -1.31512271 -1.24590573
 [9] -1.17668874 -1.10747176 -1.03825477 -0.96903779 -0.89982080 -0.83060382 -0.76138683 -0.69216985
[17] -0.62295286 -0.55373588 -0.48451889 -0.41530191 -0.34608492 -0.27686794 -0.20765095 -0.13843397
[25] -0.06921698  0.00000000  0.24694000  0.75110917  1.25527833  1.75944750  2.26361667  2.76778583
[33]  3.27195500  3.77612417  4.28029333  4.78446250  5.28863167  5.79280083  6.29697000  6.80113917
[41]  7.30530833  7.80947750  8.31364667  8.81781583  9.32198500  9.82615417 10.33032333 10.83449250
[49] 11.33866167 11.84283083 12.34700000

enter image description here

答案 1 :(得分:2)

这是与@Megatron相似的通用解决方案,但编程起来稍微容易一些。它基本上将最小和最大色标值设置为大小相等。

range <- max(abs(matrix));
pheatmap(matrix, breaks = seq(-range, range, length.out = 100))