pheatmap: adding row label instead of row names
1
1
Entering edit mode
23 months ago

I am trying to place a label "participants" beside the y-axis of a heatmap instead of the row names. Removing row names was easy with changing the default to show_rownames = F, but I did get no response to label_row when I changed it from NULL to either T of "participants". Is there a way of placing a label instead of row names?

pheatmap R • 8.2k views
ADD COMMENT
0
Entering edit mode
23 months ago
seidel 11k

The labels_row argument is used for adding labels of your choice (example below). But if you want to add text arbitrarily, you can use functions from the grid package (I think pheatmap uses this under the hood). You just have to toy around with getting the x, y coordinates right:

library(pheatmap)
library(grid)
# create some toy data
x <- as.data.frame(matrix(rnorm(15), nrow=3, ncol=5))
# set alphabetic rownames
rownames(x) <- c(letters[1:3])
# draw heatmap with our own names
pheatmap(x, labels_row=c("foo", "goo", "schmoo"))

# add text arbitrarily
grid.text("foobar", x=0.9, y=0.5, rot=270)

I guess what you want to do is avoid labels_row altogether, use show_rownames=FALSE, and add the call to grid.text().

ADD COMMENT
0
Entering edit mode

Thank you, Seidel. Yes, I want to avoid the row names, and I did that earlier: show_rownames = FALSE, which worked well. I also removed the dendrogram along the y-axis. The aim was to put the label "participants" into the position where the rownames were before. I played around with the grid.text function but I couldn't make it work. I end up getting an error message: non-numeric argument to binary operator

Maybe easier to show, here is my codeline:

pheatmap(data3, color=colorRampPalette(c("white", "yellow", "red"))(50),  legend_breaks = c(0, 500, 1000, 1500, max(data3)), 
         main = "", legend_labels = c("0", "500", "1000", "1500", "sqrt counts\n"),
         show_rownames = F, cluster_rows = FALSE)
with: +   grid.text("participants", x=0.9, y=0.5, rot=270)

I get the error message I don't know how to post an image of my heatmap but maybe that's not necessary.

ADD REPLY
0
Entering edit mode

Is the "with: + " line part of your code? That will cause an error. There should only be two separate lines to draw the figure: (1) pheatmap(stuff), and (2) grid.text(otherstuff). You don't need a plus sign. Just each function on its own line.

ADD REPLY
0
Entering edit mode

Ok, without the plus I don't get any error code and I do get a heatmap but the label 'participants' is positioned not beside the legend on the outside instead it is in the map, in the last column. It's a step in the right direction I feel. Is there a way of moving it to the outside?

ADD REPLY
0
Entering edit mode

Yes. The coordinate space of the plot is mapped from 0 to 1 for both x and y starting at the upper left corner. So simply play with the x and y numbers, and execute the function over and over again until it's where you want (0.93, 0.95, etc.). When it's right, execute both functions a last time to draw the final version. (you can execute with grid.text() over and over again, which is messy but quick and easy. In fact you can give it a vector of numbers instead of just one to try many positions at once).

ADD REPLY
0
Entering edit mode

Great, I can move it around now. Next issue I didn't think of is that pheatmap automatically places the legend next to the last column. After removing the row names it is now right beside the map. I like to move it to make room for 'participants'. Would you know how to do it?

ADD REPLY

Login before adding your answer.

Traffic: 1988 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6