Search code examples
rheatmapcomplexheatmap

Extracting row names from heatmap


I made a heatmap from my data using Complex heatmap library from R. Below is the code that I have used and also the sample data.

Data

Gene    T1  T2  T3  T4
ARL2-SNX15  4.678845561 3.728158677 4.825892144 3.189954084
PALM    2.657130448 2.880786566 3.054500641 2.408040399
AC239800.3  4.190678312 4.226734964 2.701671155 4.745703221
HBG2    12.09275318 11.943057   12.54390598 11.97291386
ALDH3B2 1.599244728 1.533113992 0.97241763  1.595816246
IGKV1-39    4.67511509  5.139282438 4.79232686  4.853376044
RNU1-2  2.833601135 2.565489873 1.982653588 2.590228834
RNU1-27P    3.006656094 2.851094423 2.135404861 3.214987282
RPL9    9.716225455 9.438792748 9.843155568 9.620418751
HBB 15.00426572 14.86490879 14.9677195  14.97970035
RAP1GAP 5.886838373 5.792277665 7.195829067 5.255034813
HBA1    14.62993733 14.40249302 14.89753465 14.48068449
TUBB1   10.27923383 10.01917144 10.34000216 10.24278332
RNF182  3.44912724  3.949744939 3.511681562 3.971171624
RPL13AP5    3.978335415 6.000867626 3.86817358  3.694021457
RBM38   11.27509196 10.89138854 11.55169865 11.20333868
RPL18AP3    3.665458082 3.191623264 2.900735037 3.084065356
IGHG1   11.23628907 11.0557336  11.13022035 11.12918895
UBB 13.81669029 13.45248731 14.08661685 13.67685017
HLA-DRB5    10.66891122 10.72396649 10.5205553  10.64595214
STRADB  10.86225572 10.67154689 11.16003312 10.74628207
MTND2P28    10.64093847 10.47184716 10.72911898 10.52005946
RAB11FIP1   11.81598906 11.49272433 11.99291682 11.67315275
NUP98   10.64093473 10.42678921 10.50026956 10.59906381
CCPG1   10.51200242 10.2721869  10.74731324 10.42695797
PLXNC1  11.60276565 11.3567064  11.66266712 11.50071436
HLA-DQB1    10.4963715  10.19214322 10.70242373 10.3567436
SIGLEC10    11.58904217 11.7499333  12.06540789 11.85295674
PYGL    12.07339319 12.08541381 12.70585451 12.13535937
FAM49B  11.33640888 11.08364601 11.26294627 11.2139601
HCAR3   10.52735471 10.35219599 10.41032798 10.5234326
RPL7AP6 3.348774801 3.355232552 3.611635236 3.636829326
ZFP36L1 11.91883538 11.60694057 12.34639317 11.80075302
USP32P1 3.006656094 2.851094423 2.135404861 3.214987282
SBDSP1  2.447640089 3.277476411 3.172198078 2.112678772
DCAF8   10.53567292 10.29122747 11.21503549 10.39643277
GTF2IP4 11.17176076 10.76951454 11.69685526 10.96960084
SLC12A6 10.99827936 10.66777147 10.95550335 10.90683492
ADGRE1  10.19958496 9.8344269   10.52018115 10.28207951
DYNC1H1 10.78783881 10.45769676 10.77325874 10.50466831
CTSA    11.57098737 11.29423254 12.00243818 11.45469475

code

library(gplots)
library(ComplexHeatmap)
mat=read.table("Matrix.txt", header=T, sep="\t", row.names = 1)
mat.z<-t(apply(mat,1,scale))
Heatmap((mat.z),  row_names_gp = gpar(fontsize = 2),cluster_rows = T, col = bluered(256),cluster_columns = T,column_labels = colnames(mat), name="Signatures")

I get a nice heatmap with rows and columns clustered. Now I want to extract the row names exactly in the order they are present in the Heatmap. How can I do that?

Thank you


Solution

  • There's also row_order-Heatmap-method:

    library(ComplexHeatmap)
    mat.z <- t(apply(mat,1,scale))
    ht <- Heatmap(mat.z)
    ht <- draw(ht)
    
    # get order though row_order-Heatmap-method:
    rownames(mat.z)[row_order(ht)]
    #>  [1] "HLA-DRB5"   "ALDH3B2"    "AC239800.3" "RNU1-27P"   "USP32P1"   
    #>  [6] "RNU1-2"     "RPL18AP3"   "IGKV1-39"   "RPL13AP5"   "RNF182"    
    #> [11] "IGHG1"      "FAM49B"     "HBB"        "SLC12A6"    "HCAR3"     
    #> [16] "NUP98"      "PALM"       "SBDSP1"     "MTND2P28"   "DYNC1H1"   
    #> [21] "ARL2-SNX15" "STRADB"     "HBA1"       "GTF2IP4"    "ZFP36L1"   
    #> [26] "CTSA"       "DCAF8"      "HBG2"       "PYGL"       "RAP1GAP"   
    #> [31] "CCPG1"      "RAB11FIP1"  "UBB"        "HLA-DQB1"   "RBM38"     
    #> [36] "ADGRE1"     "PLXNC1"     "RPL9"       "TUBB1"      "SIGLEC10"  
    #> [41] "RPL7AP6"