Put the Y axis on the left of a heatmap?

10,979

In the heatmap function the positions of the axes are hard-coded. But it would be very easy to change just a single number to get it positioned on the other side. Type "heatmap" at your console and change the first argument from a 4 to a 2 in the second axis() call.

All I changed was:

axis(2, iy, labels = labRow, las = 2, line = -0.5, tick = 0,  # the 2 used to be 4
         cex.axis = cexRow)

There still need to be changes in the margins to accommodate the switch. Changing the current value of 0 to 5 seemed to create adequate space in the example I was playing with from the help page:

...
par(mar = c(margins[1L], 5, 0, margins[2L]))

This was my test case:

x  <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start=0, end=.3)
cc <- rainbow(ncol(x), start=0, end=.3)
utils::str(hv) # the two re-ordering index vectors

## no  dendrogram (nor color strip)
heatmap.new(x, Colv = NA, Rowv=NA, col = cm.colors(256), scale="column",
        margins=c(5,2),
        xlab = "specification variables", ylab= "Car Models",
        main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
Share:
10,979
James
Author by

James

BY DAY: Information Management at Dart Neuroscience in San Diego, CA BY NIGHT: Husband and father FOR FUN: Film photography and alt printing processes

Updated on July 20, 2022

Comments

  • James
    James almost 2 years

    How do I make a heatmap with the Y axis labels on the left? It seems to default to the right. Do I need to make a custom axis using axis()?

  • James
    James over 12 years
    note: I noticed the side = arguments and changed those as well. Same result, no axis labels. Could this be a problem with the margins? The heatmap seems to be crammed into the upper left corner of the image. I'll keep playing around. I think we are on the right track here. Thanks!
  • IRTFM
    IRTFM over 12 years
    Not sure why you used 3. That would be the top. You may need to be more complete in saying what you did. My answer assumed that you knew how to create a new function from the code of an existing one. I do get a switch of sides, although you would still need to adjust the margins with par().
  • James
    James over 12 years
    I used 3 for the x axis and 2 for the y axis since I wanted them on the top and left respectively.
  • IRTFM
    IRTFM over 12 years
    I guess we will need to see your code. I got left labels. I will post the line I changed
  • James
    James over 12 years
    I have to stretch the graphic window to see it, but it works! Thanks.