Search code examples
rpheatmap

missing annotations and colors in pheatmap


Using pheatmap R package, I need to make a heatmap with annotations of a custom color set.

I use the following code:

### Make a matrix
mat<-replicate(23, rnorm(23)) 
colnames(mat)<-c("1","2","3plus3reseq","4","5","6","7","8","D1","D2","D3","11","12","13","14","15","16","59","e7","e8","e9","17","18")
rownames(mat)<-c("1","2","3plus3reseq","4","5","6","7","8","D1","D2","D3","11","12","13","14","15","16","59","e7","e8","e9","17","18")


### Define annotations
colData_D<-data.frame(time=factor(x=c(rep("0",2), rep("1.5",3),rep("3",3), rep("3D",3), rep("12", 3), 
                                      rep("24",4),rep("24e",3), rep("24c",2)),levels=c("0","1.5","3","3D","12","24","24e", "24c")))

### Assign custom colors for annotations
newCols <- colorRampPalette(grDevices::rainbow(length(unique(colData_D$time))))
mycolors <- newCols(length(unique(colData_D$time)))
names(mycolors) <- unique(colData_D$time)
mycolors <- list(category = mycolors)


### Make the heatmap
pheatmap(mat,annotation_col = colData_D, annotation_colors = mycolors)

This produces a heatmap where some annotations are missing, and colors are not changed to custom color scheme.

enter image description here

However if I change the column and row names, the missing categories appear though colors are still assigned to default ones.

mat<-replicate(23, rnorm(23)) 
colnames(mat)<-c(1:23)
rownames(mat)<-c(1:23)

pheatmap(mat,annotation_col = colData_D, annotation_colors = mycolors)

enter image description here

Any hint is greatly appreciated.


Solution

  • You need the names of the list "annotation_colors" needs to match your colnames in colData_D. And also give the rownames of your annotation dataframe the colnames of your matrix

    So do:

    rownames(colData_D) = colnames(mat)
    mycolors <- newCols(length(unique(colData_D$time)))
    names(mycolors) <- unique(colData_D$time)
    mycolors <- list(time = mycolors)
    pheatmap(mat,annotation_col = colData_D, annotation_colors = mycolors)
    

    Is this what you wanted?

    enter image description here