I was plotting a ChIP-seq data using the pheatmap, see code below:
km<- kmeans(m1,2) # determin how many cluster you want, I specify 2 here m.kmeans<- cbind(m1, km$cluster) # combine the cluster with the matrix dim(m.kmeans) #  903 602 # the last column is 602 o<- order(m.kmeans[,602]) # order the last column m.kmeans<- m.kmeans[o,] # order the matrix according to the order of the last column pheatmap( m.kmeans[,1:601], cluster_rows = F, cluster_cols = F, col= hmcols, breaks = bk, legend=FALSE, show_rownames=FALSE, show_colnames=FALSE)
It works fine for me, I clustered the data to two groups by specifying K=2, the problem is that group 1 sometimes shows up in the upper part of the heatmap, sometimes it shows up in the bottom part of the figure if I plot it several times.
I think it has to do with the assignment of the group number, say, the first group is assigned to 1, the other is assigned to 2. However, next time if you plot the same data, the first group assigned to 2, the other is assigned to 1. R randomly assigns the number to the groups.
How can control this?
Thank you every much!