Created
April 18, 2024 11:30
-
-
Save abikoushi/fd8556e1978921016a3444172af52cb1 to your computer and use it in GitHub Desktop.
set color-bar's mid-point to 0 (pheatmap)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(pheatmap) | |
# length(breaks) == length(paletteLength) + 1 | |
# use floor and ceiling to deal with even/odd length pallettelengths | |
mid0scale <- function(test, paletteLength=120){ | |
myColor <- colorRampPalette(c("orange", "white", "royalblue"))(paletteLength) | |
myBreaks <- c(seq(min(test), 0, length.out=ceiling(paletteLength/2) + 1), | |
seq(max(test)/paletteLength, max(test), length.out=floor(paletteLength/2))) | |
return(list(color=myColor, breaks=myBreaks)) | |
} | |
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 = "") | |
col <- mid0scale(test) | |
pheatmap(test, color=col$color, breaks = col$breaks) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment