Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created April 19, 2024 04:15
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/de05a5ca5665116635cc3a2f2fca2b26 to your computer and use it in GitHub Desktop.
Save abikoushi/de05a5ca5665116635cc3a2f2fca2b26 to your computer and use it in GitHub Desktop.
pheatmap: colored row labels text-by-text
library(pheatmap)
library(grid)
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))
}
set.seed(123)
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 = "")
ann <- data.frame(even = as.integer(1L:20L %% 2L == 0L))
row.names(ann) <- row.names(test)
fcol <- mid0scale(test)
p <- pheatmap(test,
color=fcol$color,
breaks = fcol$breaks,
annotation_row = ann)
cols = c("steelblue","firebrick")
rcols = rep(cols,each=10)
p$gtable$grobs[[5]]$gp=gpar(col=rcols)
png("heat.png")
print(p)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment