rheatmappheatmap

order pheatmap by annotation


I am attempting to make a figure using the package pheatmap. I would like to group the data by the annotation that I am using, but it won't group together.

My data is here:

sub_samp <- structure(c(1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 
0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 
1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 
1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(20L, 
9L), .Dimnames = list(c("GQQG", "RAPM", "RGQI", "GYNY", "SPTGGSYNX", 
"SRLPSDX", "SLXVAGTNEK", "RXVQYGTDT", "SLTXASVE", "SRXGTSGRADE", 
"QLDY", "XPRHQTYE", "SXPGTGKSA", "PAVS", "SPRXVTE", "SRDXGPNYG", 
"TGTXLE", "PEAH", "SLQGGXT", "SPSARDRVGX"), c("pt11_protected", 
"pt12_protected", "pt14_protected", "pt15_protected", "pt16_protected", 
"pt17_protected", "pt18_protected", "pt19_protected", "pt20_protected"
)))

sub_anno <- structure(list(seq_share = c("shared", "shared", "shared", "shared", 
"shared", "shared", "unshared", "unshared", "unshared", "unshared", 
"unshared", "unshared", "unshared", "unshared", "unshared", "unshared", 
"unshared", "unshared", "unshared", "unshared")), .Names = "seq_share", row.names = c("XPRHQTYE", 
"GQQG", "GYNY", "QLDY", "RGQI", "SRLPSDX", "PAVS", "PEAH", "RXVQYGTDT", 
"RAPM", "SXPGTGKSA", "SLXVAGTNEK", "SLQGGXT", "SLTXASVE", "SPRXVTE", 
"SPSARDRVGX", "SPTGGSYNX", "SRXGTSGRADE", "SRDXGPNYG", "TGTXLE"
), class = "data.frame")

I am using the above data (derived from dput(xyz_data)) and running the following code for the pheatmap

pheatmap::pheatmap(sub_samp, annotation_row = sub_anno)

How can I get the data to group together by the annotation?


Solution

  • To specify a predetermined row order in pheatmap you should first turn off the row clustering and then manually order your matrix. Using the data you provided this is fairly straight forward.

    # 1) reorder the matrix based in the annotation
    sub_samp_ordered <- sub_samp[rownames(sub_anno), ]
    
    # 2) plot heatmap with no row clusters
    pheatmap::pheatmap(sub_samp_ordered, annotation_row = sub_anno, cluster_rows = F)
    

    Which results in: enter image description here