Change axis labels rotation of a heatmap generated by package pheatmap
Asked Answered
M

0

7

The x-axis labels of heatmaps produced by package pheatmap are 270 degrees rotated by default. I need to make them 90 degrees rotated.

I have traced the pheatmap() function and see there is an internal (invisible) function that produces labels:

draw_colnames <- function (coln, ...) 
{
    m = length(coln)
    x = (1:m)/m - 1/2/m
    grid.text(coln, x = x, y = unit(0.96, "npc"), vjust = 0.5, 
              hjust = 0, rot = 270, gp = gpar(...))
}

I simply changed the rot = 270 by rot = 90 and also hjust = 0 by hjust = 1 in above function using the following command, and it worked:

fixInNamespace("draw_colnames","pheatmap")

But the problem is that fixInNamespace() permanently modifies the function definition in the package. I rather would be more happy not to alter the original function definition, but temporarily replace the definition of draw_colnames() function by my own one just in cases that I need.

Is there any solution?

Maggard answered 13/10, 2013 at 11:55 Comment(7)
What happens if you just source() your modified function?Voluptuary
I thought that might be the case, recent changes in the namespace operations have made this more difficult. Since what you already did modified your copy of the package, be sure that you reinstall the package when testing different approaches. Otherwise you are testing on your already modified package. Here's another approach: Make a copy of the pheatmap function and change draw_colnames to draw_colnames2. Then copy draw_colnames and modify as you did, and rename to ...2. Then try using your modified plot function and see what happens.Voluptuary
@BryanHanson, Thanks. The problem is that draw_columns() is not directly called in the visible pheatmap() function, but called in another invisible function heatmap_motor(). So if I want to do the task in this approach I need to redefine pheatmap2(),heatmap_motor2() and draw_colnames2(). Is there an easier way?Maggard
That's too bad. Good detective work on your part. I'm out of ideas just now. At the minimum, suggest to the package author that rot be removed so that users can pass their own values in a future version.Voluptuary
pheatmap is on Github. You can clone it and modify it and build it locally, then if it works offer it back to the author.Voluptuary
@BryanHanson That's a smart solution. Still I am eager to learn a bit more from R by doing the task on the flight, without modifying the source of original package.Maggard
This is an excellent solution. For my case, I needed to avoid rotating the plot label, since my column names are just one-letter long, so I set rotation to 0. Then, I changed the vjust from 0.5 to 1 to move my labels farther away from the bottom row. After quitting and restarting R Studio, the namespace was reset.Chong

© 2022 - 2024 — McMap. All rights reserved.