2

I have a small question about expression when I use it in the pheatmap.

For example, I want to add information in the main title in a pheatmap plot with something in details. Here is my reproducible code:

# main <- expression(bold(paste(
#   atop(CD4_CD69^"+", 
#        "\nReported human signature core methylation genes"~n[tiles]~" = 1999;
#        "~n[genes]~"= 89")
#   )))

main <- expression(bold(paste(
  CD4_CD69^"+", 
  "\nReported human signature core methylation genes", 
  "\n", n[tiles], " = 1999;", n[genes], "= 89"
  )))   

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) <- c(paste("CGene", 6:10, sep=""), 
                    paste("AGene", 1:5, sep=""), 
                    paste("BGene", 11:15, sep=""), 
                    paste("DGene", 16:20, sep=""))


library(pheatmap)

p1 <- pheatmap(test, main="pheatmap")

p2 <- pheatmap((test), cluster_row=FALSE, 
               main=expression(bold(paste(
                 atop(CD4_CD69^"+", 
                      "Reported human signature core methylation genes"~n[tiles]~" = 1999;
                      "~n[genes]~"= 89")
                 ))))

It works well though with a little bit error information.

But here, the main title information is long, so it seems that add two new lines to split the sentence into three lines is better.

However, as we all know, "\n" doesn't work in expression. Though I can remove expression here.

But I don't know the other better methods to solve my problem.

I hope somebody or experts could give me some advice or solutions.

Thanks in advance.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
花落思量错
  • 352
  • 1
  • 11

1 Answers1

0

One option is to use this workaround:

Create plot without title and then add multiline text with grid and gridExtra packages:

library(pheatmap)
library(grid)
library(gridExtra)

# heatmap without a main title
p2 <- pheatmap(test, cluster_row = FALSE)

# multiline title
title_grob <- textGrob(label = "CD4_CD69+\nReported human signature core methylation genes\nn[tiles] = 1999; n[genes] = 89",
                       gp = gpar(fontsize = 14))


grid.newpage()
grid.draw(arrangeGrob(title_grob, p2$gtable, heights = c(1, 10)))

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66