pheatmap: how to avoid fontface, fontsize, and color from writing over existing rownames
0
0
Entering edit mode
10 weeks ago

Is there a way to get pheatmap (in R) to not write over the existing row_names if I were to change fontface (ie. bold), fontsize (i.e. size = 20), and color (i.e. blue) of the rows?

By default, the color of the row or column text is black when making a pheatmap, but with a little tweaking, one can change the color of the text as I learned by this post: How to change color of rownames display in pheatmap; however, the code just writes over existing rownames. So, if you were to do anything aside from a color change (ex. increase font or change the fontface), then you see the issue - the rownames in black at the default font and fontface are still very visible in the background. Is there a way to not have the default font showing in the background?

Here is a reproducible example:

library(pheatmap) ; library(grid)

data <- replicate(16, rnorm(16))
rownames(data) <- paste0("OTU", c(1:nrow(data)))
colnames(data) <- paste("Sample", c(1:ncol(data)))
data = as.data.frame(data)
data$regulation = rep(c("up", "down"), each = 8) data$colors = ifelse(data$regulation == "up", "red", "blue") #Graphing p = pheatmap(data[, 1:16]) cols = data[order(match(rownames(data), p$gtable$grobs[[5]]$label)), ]$colors #Assuming row labels are in grob 5 p$gtable$grobs[[5]]$gp = gpar(col = cols, fontsize = 15, fontface = "bold")
p


Here is the graph:

In attempts to do so, I've set the show_rownames = FALSE and all this does is over-ride the code and no rownames are written at all.

fontsize rows color R pheatmap • 390 views
0
Entering edit mode

Do you absolutely need to stick to pheatmap? ComplexHeatmap is a much better, easily customizable package that grew out of expanding pheatmap.

0
Entering edit mode

use the same font size in pheatmap object (p) and clear graphics device.

p = pheatmap(data[, 1:16], fontsize_row = 15)
dev.off()
cols = data[order(match(rownames(data), p$gtable$grobs[[5]]$label)), ]$colors  #Assuming row labels are in grob 5
p$gtable$grobs[[5]]$gp = gpar(col = cols, fontsize = 15, fontface = "bold") p  ADD REPLY 1 Entering edit mode How does this work? I would love to understand how turning off the graphics device translates to some new functionality in R. ADD REPLY 0 Entering edit mode This worked absolutely beautifully. Thank-you. Yes; I see a common theme here to check out ComplexHeatmap. Bc if it's more user friendly - and we aren't pulling rabbits outta hats (i.e. turning off graphics in the middle of graphing) to do simple things like this - it would be totally worth it. Thank-you again, cpad0112! ADD REPLY 0 Entering edit mode What I personally love about ComplexHeatmap is its architecture - each object is designed the way a good Object Oriented Language would do it (which emulates real life very well) - You create a HeatmapList consisting of multiple Heatmaps with any number of ColumnAnnotations and RowAnnotations, and draw them one after the other vertically or horizontally. There is no $gtable$grobs, there's something like hmp_object$labels[hmp_object$row_order]. Things make sense intuitively. ADD REPLY 0 Entering edit mode Code works even if you remove dev.off(). p = pheatmap(data[, 1:16], fontsize_row = 15) cols = data[order(match(rownames(data), p$gtable$grobs[[5]]$label)), ]$colors #Assuming row labels are in grob 5 p$gtable$grobs[[5]]$gp = gpar(col = cols, fontsize = 15, fontface = "bold")
p