Jonas
Jonas

Reputation: 1629

R gplots: Heatmap with side colours

I want to create a heatmap using the heatmap.2 function from the gplots package. This is a minimal example.

require(gplots)

# create symmetric matrix
x = matrix(rnorm(100), nrow=10)
diag(x)         <- 1
x[upper.tri(x)] <-  t(x)[upper.tri(x)]
colnames(x)     <- rownames(x) <- letters[1:nrow(x)]

# create side colours
varcols = setNames(rainbow(nrow(x)), rownames(x))

# create heatmap
heatmap.2(x,
          symm = TRUE,             
          trace = "none",          
          revC=TRUE, # <-- THIS IS THE PROBLEM
          ColSideColors = varcols,
          RowSideColors = varcols
          )  

The problem are the sidecolors. x is a symmetric matrix, thus columns and rows should have the same sidecolors. This is fine as long as revC = FALSE. However, when I use revC = TRUE the order of the colors is messed up. Sometimes - in small examples - it helps to reverse the ColSideColors, but that doesn't always work.

Am I doing anything wrong or is this a gplots bug?

Upvotes: 2

Views: 801

Answers (1)

TAndrews
TAndrews

Reputation: 41

For anyone else who comes across this problem this is how I solved it:

thing = heatmap.2(my_matrix,...RowSideColors=row_cols, revC=F)
ordinary_order = thing$rowInd
reversal = cbind(ordinary_order, rev(ordinary_order))
rev_col = row_cols[reversal[,2]]; rev_col = rev_col[order(reversal[,1])];

heatmap.2(my_matrix, RowSideColors=rev_col, revC=T)

Upvotes: 4

Related Questions