Set 0-point for pheatmap in R

落爺英雄遲暮 提交于 2020-05-10 08:49:50

问题


How can you set the 0-point on the color scale to white in this heatmap? Does it use the breaks parameter?

In the following code, white is set to 3 (or nearby on the scale):

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


回答1:


This can be solved using breaks. Breaks correspond to numerical ranges for the color palette's bins.

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)

To see the contents of these vectors:

> 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



来源:https://stackoverflow.com/questions/31677923/set-0-point-for-pheatmap-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!