1

I am trying to add the color side bar to the heat map columns.

My cases/samples belongs to different groups/classes (like e.g. samples are the results of many genes expression in various cells and groups are different species).

I would like the color side bar to reflect different species. The column are labelled already by the type of the cell. I know that I can assign different colors to different species (using if and else) one by one but having more than 20 species and over 200 samples it is not practical. Same it is not practical to sort the data by the group and assign the different colors to e.g. samples 1 to 10, 11 to 25, etc...

I have found that as.character(as.numeric(data[,XXX]), where XXX is the column with my groups names works fine. However it delivers appalling colors. Any suggestion how to apply e.g. just a few shades from rainbow colors to it?

colCol from heatmap.2 seemed to be suitable but when used it is ignoring the same class/group and assigning e.g. the palette of colors across all the samples...

Further to the above I am wondering how to do the same when just the dendrogram is plotted. It seems to be even more complicated... Will appreciate any help!

Tal Galili
  • 24,605
  • 44
  • 129
  • 187
AussieAndy
  • 101
  • 2
  • 11

1 Answers1

2

If I understood you correctly, you could try something like this:

library(gplots)
library(dendextend)

# sample data
x  <- as.matrix(mtcars)

# determine colors & palettes
colClusters <- as.integer(nchar(names(mtcars)) == 2)+1L
rowClusters <- as.integer(factor(substr(rownames(x), 1, 1)))
colCols <- rainbow(2)
rowCols <- rainbow(10)

# create dendrograms
colDend <- x %>% t %>% dist %>% hclust %>% as.dendrogram 
rowDend <- x %>% dist %>% hclust %>% as.dendrogram 

# plot heatmap
heatmap.2(x, 
          Colv = colDend %>% color_branches(col=colCols[colClusters[order.dendrogram(colDend)]]), 
          Rowv = rowDend %>% color_branches(col=rowCols[rowClusters[order.dendrogram(rowDend)]]),
          colCol = colCols[colClusters], 
          colRow = rowCols[rowClusters])   

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Awesome! Thank you!!! Unfortunately I have realized that applying palette colors (e.g. rainbow) is not the best option. I am assigning my own colors to the groups/classes. But can not figure out in which order the colors are applied to the rows. I thought that if I have e.g. 5 factors, and I will define colCols<-c("blue","red",black",brown","green), the blue will be applied to first factor/class/group and so on. But not sure which first / first where... In dendrogram (top row on right hand side of the heat map), in data matrix (i.e. first row)? – AussieAndy Dec 30 '15 at 21:55
  • Further to the above, it seems that the color assignment is sort of random. E.g. first color names in colCols is not assigned to first group in the data set nor the first group clustered in the dendrogram (branch on the far left). – AussieAndy Dec 30 '15 at 22:26
  • I am very late here but if you want to have linear progression of colors you can use something like colors = [(0, 0, 0), (1, 0, 0), (0, 1, 0)] n_bins = 100000 # Discretizes the interpolation into bins cmap_name = 'my_list' regn = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins) – Ando Jurai Jul 11 '17 at 16:51
  • 1
    @AndoJurai Looks like Python, but the question is about R. :) – lukeA Jul 11 '17 at 20:02