tchakravarty
tchakravarty

Reputation: 10984

Control axis labeling in R heatmap

I am trying to create a heatmap in R, but the axis labels (which uses the row.names information of the data frame that is being passed to the heatmap function) is crowding the x-axis, and I can't figure out how to control the labeling.

Here is an example:

vDates = seq.Date(from = as.Date('29-11-2012', 
                                format = '%d-%m-%Y'), 
                 length.out = 203, by = 'day')
dfHeatMap = rdirichlet(length(vDates), runif(15))
row.names(dfHeatMap) = as.character(vDates)
heatmap(t(dfHeatMap), Rowv = NA, Colv = NA, 
        col = cm.colors(256))

Crowded heatmap

Any suggestions/packages that take care of this issue?

Upvotes: 1

Views: 2877

Answers (1)

tchakravarty
tchakravarty

Reputation: 10984

I was able to figure this out by RTFM (more carefully). Initially I was not able to get the labCol and the labRow working. Here is a working example:

library(gtools)
library(ClassDiscovery)

# generate sequence of dates
vDates = seq.Date(from = as.Date('29-11-2012', 
                                 format = '%d-%m-%Y'), 
                  length.out = 203, by = 'day')

# generate the random samples
dfHeatMap = as.matrix(rdirichlet(length(vDates), runif(15)))
row.names(dfHeatMap) = as.character(vDates)


# column labels
vDatesNew = rep(as.Date(NA), length(vDates))
vDatesNew[seq(from = 1, to = 203, by = 10)] = 
  vDates[seq(from = 1, to = 203, by = 10)]

# row labels
labRow = c(NA, NA, 3, NA, NA, 6, NA, NA, 9, 
           NA, NA, 12, NA, NA, 15)

# draw the heatmap with aspect control
aspectHeatmap(t(dfHeatMap), Rowv = NA, Colv = NA, 
              col = cm.colors(256), labCol = vDatesNew, labRow = labRow,
              margins = c(5, 5), hExp = 1.5, wExp = 4)

I have used the ClassDiscovery package to control the aspect ratio of the heatmap. This is what it looks like: enter image description here

Upvotes: 1

Related Questions