1

Using the "pheatmap" package in R, with Rstudio for Windows 10, I can follow an example(as shown here) to produce a heatmap with selective coloring of row names as long as the heatmap displays both row and column names. However, if I try to generate a heatmap with selective coloring of row names without showing the column names, I get an error:

"Error in set.gpar(x$gp, grob = x) : argument must be a 'gpar' object"

Here is a borrowed example of a heatmap:

library(pheatmap)
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("Gene", 1:20, sep = "")

And here is a borrowed example of a method for coloring selected row names:

library(grid)
library(gridExtra)
library(pheatmap)
hm <- pheatmap(test, silent = TRUE)
hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
grid.draw(hm2)

Sample heatmap with selected row names colored red However, if I try to create the heatmap without showing the column names as follows:

pheatmap(test, show_colnames=FALSE, silent=TRUE)
hm$gtable$grobs[[5]]$gp$col <- rep(c("black", "red"), each = 10)
leg <- legendGrob(c("Exp1", "Exp2"), nrow = 2, pch = 15, gp = gpar(fontsize = 10, col = c("black", "red")))
hm2 <- arrangeGrob(hm$gtable, leg, ncol = 2, widths = c(5,1))
grid.draw(hm2)

Then I get an error:

Error in set.gpar(x$gp, grob = x) : argument must be a 'gpar' object

  1. Why does this error occur?
  2. How could the heatmap be produced with selective coloring of row names while not showing the column names?

Please note that I prefer to use the "pheatmap" package. Answers to similar questions have recommended using alternative packages such as the "ComplexHeatmap" package, but for various reasons, I prefer to use the "pheatmap" package if at all possible. Thank you.

aynber
  • 22,380
  • 8
  • 50
  • 63

0 Answers0