Use customised colours for annotated groups in pheatmap.
I have a list of genes that belong to different groups. I would like to plot the expression using heatmap and show different colours for each group. I will use pheatmap in R.
The *.ids contains gene ids in that group and meanExpr contain the expression values in different timepoints. I will use the following command to creat the input file for pheatmap:
for i in *.ids; do awk '{print $0, FILENAME}' $i | sed 's/\.ids//'; done | sort| join - meanExpr_male.txt |sort -k2 > expression_male.txt
library(pheatmap)library(RColorBrewer)# in this script the 2nd column in the expression file is the group namesexp<-read.delim("expression_male.txt",sep=" ",header=T)z<-exp[,-1:-2]rownames(z)<-exp[,1]zz<-as.matrix(z)# add group annotationanno<-data.frame(row.names=exp$id,Group=exp$group)# creat colours for each groupnewCols<-colorRampPalette(grDevices::rainbow(length(unique(anno$group))))annoCol<-newCols(length(unique(anno$group)))names(annoCol)<-unique(anno$group)annoCol<-list(category=annoCol)# make the heatmappheatmap(zz,scale="row",cluster_cols=F,cluster_rows=F,annotation_row=anno,annotation_colors=annoCol,cellheight=10,cellwidth=10,file="expr_group.png")
The heatmap looks like this:
expr1
Then I would like to define the colour for each group:
1
2
3
4
5
6
7
...# add group annotationanno<-data.frame(row.names=exp$id,Group=exp$group)# define the coloursannoCol<-list(Group=c(group_1="blue",group_2="red",group_3="orange",group_4="grey"))...
expr2
Another thing is that some special characters like “+ -” are not allowed in R variables but commonly used in biology, e.g. Co-chaperone. In this case, we need to force R to use that variable name by adding backticks, e.g.,