How to change color of rownames display in pheatmap
3
2
Entering edit mode
3.9 years ago
helen ▴ 70

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 = ???)
R pheatmap • 11k views
ADD COMMENT
5
Entering edit mode
3.9 years ago

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

Rplot

ADD COMMENT
1
Entering edit mode

what is [[5]] in your code

ADD REPLY
0
Entering edit mode

Thank you so much, cpad0112! This pheatmap is pretty much what I need. I'll try to modify your example.

ADD REPLY
3
Entering edit mode
3.9 years ago

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.

ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode
2.4 years ago
MCH ▴ 10
p = pheatmap(whatever)
p[[4]]$grobs[[2]]$gp$col = 'red'
plot(p)
ADD COMMENT
0
Entering edit mode

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

pheatmap

ADD REPLY
0
Entering edit mode

enable row names

ADD REPLY

Login before adding your answer.

Traffic: 2322 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6