Thank you so much, cpad0112! This pheatmap is pretty much what I need. I'll try to modify your example.
The row name labels of my heatmap are genes. The default color for the column names and row names are black, however, I would like to change some gene names to different colors (for example, red for up-regulated genes and blue for down-regulated genes, similar to volcano plot)
It seems that there is no function in pheatmap can change this setting. The most possible one is labels_row to custom labeled row names. Does anyone know how to custom row name color?
pheatmap(test, cellwidth=30, , cellheight = 10, angle_col = 45, annotation_col = df, labels_row = ???)
3 answers
It's tricky. here is an example. Please note that data frame is created from code given in: Is it possible to italicize row names with pheatmap()?
library(grid)
library(pheatmap)
data <- replicate(10, rnorm(10))
rownames(data) <- paste0("GENE", c(1:nrow(data)))
colnames(data) <- paste("Sample", c(1:ncol(data)))
data=data.frame(data)
data$status=rep(c("up","down"), each=dim(data)[1]/2)
data$colors=ifelse(data$status=="up","green","red")
e=pheatmap(data[,1:10])
cols=data[order(match(rownames(data), e$gtable$grobs[[5]]$label)), ]$colors
e$gtable$grobs[[5]]$gp=gpar(col=cols)
e
what is [[5]] in your code
I recommend taking a look at ComplexHeatmap, which can be customized in pretty much any way to your heart's content. It also has a pheatmap function that allows you to use the exact same code in addition to any of ComplexHeatmap's capabilities.
Thank-you for this great example; however, after running this code, I noticed that after running the code e=pheatmap(data[, 1:10]), that it graphed the above graph and the y-axis text was still printed, but in black. After running the rest of the code, to change the color of the text, it just added another layer of text over the black text with the desired colors. This can cause issues when one has many rows as the text looks squished and not very clear. If one were to include a change of the size or font (ex. gpar(fontsize = 20 or fontface = "bold), then the issue is very clear. You'll have a layer of black text under the new colored text and it is very messy.
Is there anyway to avoid pheatmap doing this? I tried doing show_rownames = FALSE, but this overrides the codes and no text is printed at all.
Please open up a new question and post your full code, resulting image, and what you hope to achieve. Piggybacking on questions tends to clutter the site and will make it tougher for others with your issue to find your question.
Not a problem at all and I apologize for the error. Here is the link: pheatmap: how to avoid fontface, fontsize, and color from writing over existing rownames.
p = pheatmap(whatever)
p[[4]]$grobs[[2]]$gp$col = 'red'
plot(p)
Did you try this code before posting? Run following example and check what your code does:
libraries
library(grid)
library(pheatmap)
Data frame
data <- replicate(10, rnorm(10))
rownames(data) <- paste0("GENE", c(1:nrow(data)))
colnames(data) <- paste("Sample", c(1:ncol(data)))
data=data.frame(data)
Your code
p = pheatmap(data)
p[[4]]$grobs[[2]]$gp$col = 'red'
p
output
enable row names
Log in to answer this question.
