rsortingheatmappheatmap

R pheatmap determine column order


Hi Stack Overflow community,

I am creating heatmap using pheatmap() and have difficulty ordering columns in a sorted order. My data 'matrix' is already sorted in the desired order for display on the plot.

I tried save my desired order (ascending order) into 'col_order' and use 'column_order = col_order' as an option in pheatmap(). However, it doesn't work. I also tried set 'cluster_cols = F' but R returns error message 'formal argument "cluster_cols" matched by multiple actual arguments'

I appreciate any comment!

Here is my code:

pheatmap(matrix,
         annotation_row=groupmatrix,
         cluster_cols = T,
         color = colorRampPalette(c("blue", "white", "red"))(50),
         show_colnames = T,
         scale="row",  
         border_color ="NA",
         fontsize =8,
         fontsize_row=6,
         fontsize_col=6,
         treeheight_row = 0,
         treeheight_col = 0, # remove dendrogram
         cluster_rows = F,
         cluster_cols = F)

Here is what my plot looks like right now, the x-axis order is all mess up and I want it to go from 1 to 48 in order.

enter image description here


Solution

  • Try this: Remove the cluster_cols = TRUE in the third row:

    Here is an example:

    library(pheatmap)
    
    test = matrix(rnorm(200), 20, 10)
    test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
    test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
    test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
    colnames(test) = paste("Test", 1:10, sep = "")
    rownames(test) = paste("Gene", 1:20, sep = "")
    
    
    pheatmap(test,
             color = colorRampPalette(c("blue", "white", "red"))(50),
             show_colnames = T,
             scale="row",  
             border_color ="NA",
             fontsize =8,
             fontsize_row=6,
             fontsize_col=6,
             treeheight_row = 0,
             treeheight_col = 0, # remove dendrogram
             cluster_rows = F,
             cluster_cols = F)
    

    enter image description here