0

I am trying to plot a pheatmap. However, unlike other plotting packages in R like ggplot, I am unable to find a option to add row labels and column labels. For example, in ggplot, I can add rowlab using ylab() and column label using xlab(). Here is the minimum working example:

library(pheatmap)
library(reshape2)

df = data.frame(r = c(1:3), foo = c("a","b", "c"), val = c(5,10,15), stringsAsFactors = F)
heatmat = reshape2::acast(df, foo~r, fill = 0, value.var = "val")

# Create Heatmap
p <- pheatmap(heatmat)

The default result is as follows: enter image description here However, I want to have following plot: enter image description here

Any suggestions how to add labels in the pheatmap?

Rahi
  • 135
  • 1
  • 13

1 Answers1

2

The problem is that pheatmap creates a new page each time you run it. What you can do is like in this post: x axis and y axis labels in pheatmap in R :

library(pheatmap)
library(reshape2)
library(grid)

setHook("grid.newpage", function() pushViewport(viewport(x=1,y=1,width=0.9, height=0.9, name="vp", just=c("right","top"))), action="prepend")
p <- pheatmap(heatmat)
setHook("grid.newpage", NULL, "replace")
grid.text("r", y=0.9, gp=gpar(fontsize=16))
grid.text("foo", x=-0.07, rot=90, gp=gpar(fontsize=16))

Output:

enter image description here

You can change the position and fontsize to whatever you want.

Quinten
  • 35,235
  • 5
  • 20
  • 53
  • However, any suggestion to save this plot. – Rahi Jun 07 '22 at 14:54
  • 1
    @Rahi, check this post for saving `pheatmap`: https://stackoverflow.com/questions/43051525/how-to-draw-pheatmap-plot-to-screen-and-also-save-to-file – Quinten Jun 08 '22 at 07:42