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?

Charles
  • 50,943
  • 13
  • 104
  • 142
Ali
  • 9,440
  • 12
  • 62
  • 92
  • What happens if you just `source()` your modified function? – Bryan Hanson Oct 13 '13 at 13:02
  • 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. – Bryan Hanson Oct 13 '13 at 13:12
  • 2
    @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? – Ali Oct 13 '13 at 14:03
  • 1
    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. – Bryan Hanson Oct 13 '13 at 14:05
  • 1
    `pheatmap` is on [Github](https://github.com/raivokolde/pheatmap). You can clone it and modify it and build it locally, then if it works offer it back to the author. – Bryan Hanson Oct 13 '13 at 14:09
  • @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. – Ali Oct 13 '13 at 14:37
  • 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. – David C. Dec 06 '16 at 00:44

0 Answers0