Rahi
Rahi

Reputation: 175

How to add labels to rows and columns in pheatmap along with row names and column names?

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?

Upvotes: 0

Views: 2053

Answers (1)

Quinten
Quinten

Reputation: 41235

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.

Upvotes: 2

Related Questions