Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created April 18, 2024 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abikoushi/fd8556e1978921016a3444172af52cb1 to your computer and use it in GitHub Desktop.
Save abikoushi/fd8556e1978921016a3444172af52cb1 to your computer and use it in GitHub Desktop.
set color-bar's mid-point to 0 (pheatmap)
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