rplotheatmapplotmathgplots

Make row labels italic in heatmap.2


I'm trying to make my row labels italic using the R function heatmap.2. There's no default option and I can't figure out a work around by setting par(font=3) for example. How can I set my row labels to be italic in heatmap.2?

set.seed(123)
data = matrix(sample(100), nrow=10, ncol=10)
rownames(data) = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")

library(gplots)

heatmap.2(data,
           Colv=TRUE,
           Rowv=TRUE,
           xlab=NA,
           ylab=NA)

Solution

  • You can use the labCol and labRow arguments to heatmap.2 to pass in the labels. We just need to figure out how to pass these in as a list of plotmath expressions. I always find this painful, as I don't do it often enough to remember the appropriate incantations, but was able to put the code together by adapting this R-help answer. Note that the matrix needs to have column names, which I've added in the code below

    library(gplots)
    
    # Fake data
    set.seed(123)
    data = matrix(sample(100), nrow=10, ncol=10)
    rownames(data) = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
    colnames(data) = 1:ncol(data)
    
    heatmap.2(data,
              Colv=TRUE,
              Rowv=TRUE,
              labCol=as.expression(lapply(colnames(data), function(a) bquote(italic(.(a))))),
              labRow=as.expression(lapply(rownames(data), function(a) bquote(italic(.(a))))),
              xlab=NA,
              ylab=NA)
    

    enter image description here