rggplot2cluster-analysisheatmappheatmap

Keep column AND row order in a data set EXACTLY the same as in the HEATMAP


I have a gene expression analysis z-score data frame with the gene names are rows (see dput) and the tissue as columns. The last column ("Cluster") is the result of PAMClustering. How to project the results of this table in a heatmap that looks like the example below?

enter image description here

> dput(data1[50:70,1:45])
structure(list(Gene.Name = c("GRIN1", "IFNA2", "TPO", "CASP10", 
"IFNA17", "PITX2", "PLA2G5", "EPO", "PTPN5", "WNT10B", "GNG7", 
"CALML3", "FN1", "COL1A1", "IGFBP3", "ITGB6", "PLAU", "WNT10A", 
"GSK3B", "LEFTY1", "MECOM"), T5 = c(-3.69811148837209, -3.60101581395349, 
-4.10530697674418, -3.41625488372093, -4.28083837209303, -2.70376623255814, 
-3.21424325581395, -3.21984418604651, -2.87764069767442, -3.31283209302326, 
-3.37168674418605, -3.78782255813954, 1.93293860465116, 0.806671627906981, 
0.630532558139533, -0.0694525581395347, 2.43354674418605, 2.49590441860465, 
1.6462588372093, 3.22008255813953, 1.23862930232558), T3 = c(-2.07790948837209, 
-2.68862581395349, -1.80060697674418, -2.60340488372093, -2.36844837209303, 
-4.15394923255814, -2.66443325581395, -2.08506418604651, -1.21235069767442, 
-2.44109209302326, -2.76742674418605, -2.46039255813954, 2.54423860465116, 
1.08617162790698, 1.22465255813953, 1.80700744186046, 2.33469674418605, 
0.615734418604649, 1.3833888372093, -2.10915744186047, 0.391219302325581
), T4 = c(-0.574429488372092, -1.28468581395349, -0.659696976744184, 
-0.61450488372093, -2.37953837209302, -2.16504623255814, -1.26049325581395, 
-3.09616418604651, -2.92389069767442, -2.77411209302326, -1.62651674418605, 
-3.47149255813954, -0.148651395348839, -1.12187837209302, -1.35904744186047, 
-0.613472558139535, -1.02019325581395, 1.77456441860465, 2.1520988372093, 
-0.179137441860466, 1.29817930232558), T6 = c(-1.05316948837209, 
-2.44149581395349, -2.13843697674419, -1.54891488372093, -3.12131837209303, 
-1.32185623255814, -1.00226325581395, -2.83793418604651, -1.08070069767442, 
-1.93092209302326, -1.52028674418605, -0.891332558139536, 0.647998604651162, 
-1.86877837209302, -2.06840744186047, -0.355252558139535, 0.0223067441860465, 
0.801464418604649, 1.5139488372093, 0.0487125581395338, 3.11616930232558
), T10 = c(-1.33557948837209, -2.53126581395349, -2.22820697674418, 
-1.53915488372093, -2.40372837209303, -4.41162523255814, -2.18514325581395, 
-2.02081418604651, -1.58551069767442, -2.02069209302326, -2.02509674418605, 
-1.25864255813954, -1.34461139534884, -1.89698837209302, -0.0482574418604678, 
1.35587744186047, -1.48250325581395, -0.320015581395351, 1.1838188372093, 
4.49810255813953, 0.974639302325581), T1 = c(-1.07331948837209, 
-1.87668581395349, -1.83666697674418, -1.98411488372093, -2.81954837209303, 
-3.34200623255814, -3.43745325581395, -2.27312418604651, -0.930930697674417, 
-1.95107209302326, -1.66597674418605, -1.06349255813954, 1.16470860465116, 
-0.349628372093019, 1.06254255813953, 1.48257744186046, 0.124766744186046, 
0.427674418604648, 1.5290688372093, -0.240627441860466, 1.44687930232558
), T2 = c(-2.57738948837209, -2.55067581395349, -1.92569697674418, 
-2.29553488372093, -2.90857837209303, -1.84607623255814, -2.94152325581395, 
-2.77719418604651, -1.86796069767442, -1.45514209302326, -2.04451674418605, 
-2.73748255813954, 1.60877860465116, 0.209771627906981, 0.995002558139532, 
0.705487441860465, 1.24432674418605, 1.70787441860465, 1.0594388372093, 
-1.57066744186047, -1.47972069767442), T9 = c(-1.94058948837209, 
-2.32891581395349, -1.37377697674418, -1.76565488372093, -2.81609837209303, 
-2.20927623255814, -3.52711325581395, -2.14039418604651, -1.31604069767442, 
-2.30377209302326, -1.40771674418605, -2.73811255813954, 2.13983860465116, 
1.06537162790698, 1.18011255813953, 2.42876744186046, 1.72977674418605, 
0.643824418604648, 1.5788088372093, 0.482342558139534, -0.962210697674419
), T11 = c(0.0221905116279073, -0.78117581395349, 0.521883023255815, 
-0.52603488372093, -2.46099837209303, -0.661536232558139, -1.34194325581395, 
-0.370264186046513, -2.00534069767442, -0.855562093023257, -0.859976744186046, 
-0.745592558139536, 2.81633860465116, 2.25707162790698, 2.52212255813953, 
3.10596744186047, 3.44268674418605, 3.61770441860465, 1.6035188372093, 
-0.145117441860466, 0.650379302325581), T7 = c(-0.445699488372093, 
-0.41898581395349, -2.11592697674419, -1.42687488372093, -1.51384837209303, 
-2.29934623255814, -0.809833255813954, -1.23046418604651, -1.47323069767442, 
-0.323452093023257, -1.08274674418605, -1.60579255813954, 0.808548604651161, 
1.13657162790698, 1.15050255813953, 3.65736744186046, 1.82532674418605, 
2.01465441860465, 1.2445688372093, -1.57647744186047, 0.729979302325581
), T12 = c(0.103090511627907, -0.97489581395349, -0.581636976744185, 
-0.192824883720931, -1.39945837209303, 0.512473767441861, -0.176983255813954, 
-0.531114186046513, -0.358840697674418, -0.248592093023257, -0.383396744186046, 
-0.0498125581395357, 4.20933860465116, 3.55527162790698, 2.20692255813953, 
2.00028744186047, 2.17669674418605, 0.555174418604649, -0.252481162790697, 
-1.91665744186047, 0.516269302325581), T8 = c(-0.693109488372093, 
-2.40336581395349, -2.10030697674418, -0.733184883720931, -0.623758372093025, 
-0.698766232558139, -1.05724325581395, -0.407484186046513, -1.04257069767442, 
-0.307832093023257, -1.06712674418605, -0.782812558139535, 1.63955860465116, 
1.33757162790698, 1.04011255813953, 1.61951744186047, 0.150626744186047, 
0.601434418604649, 2.1512588372093, 4.75020255813953, 1.58513930232558
), T20 = c(-0.0994394883720933, 0.0506541860465104, 0.510433023255815, 
0.265825116279069, 0.610011627906974, -0.0880262325581391, 0.188496744186046, 
-0.136974186046513, 0.181149302325583, 0.302907906976743, 0.289043255813954, 
0.122927441860464, 3.43563860465116, 2.41467162790698, 0.993382558139533, 
2.03904744186047, 0.558776744186047, -0.166335581395352, 0.533488837209303, 
0.333982558139534, 0.866439302325581), T14 = c(0.189460511627908, 
-0.198865813953489, 0.104193023255815, 0.171755116279069, -0.178248372093026, 
0.242703767441862, 0.434136744186046, 0.282435813953487, 0.232319302325583, 
0.347327906976743, -0.140156744186046, 0.336797441860464, 1.61952860465116, 
0.999271627906982, 1.51052255813953, -0.390152558139535, -0.104303255813953, 
-0.0666855813953511, -0.0386911627906974, 1.40492255813953, 1.37647930232558
), T15 = c(-0.692119488372093, -0.632985813953489, -0.651856976744185, 
-0.699774883720931, -1.36171837209303, 0.387113767441861, -0.428223255813954, 
-0.306964186046513, 0.213229302325582, -0.444342093023257, -0.181266744186046, 
0.0546774418604645, 1.45294860465116, 0.763271627906981, 1.56112255813953, 
0.116217441860464, -0.150693255813954, -0.877965581395351, -0.353901162790697, 
1.84178255813953, 1.65270930232558), T19 = c(0.395450511627907, 
1.00712418604651, 1.59968302325581, 0.966805116279069, 1.15737162790697, 
0.657273767441861, 1.18331674418605, 0.347645813953488, 1.00534930232558, 
0.807197906976743, 0.817293255813953, 0.109817441860464, 0.740778604651162, 
1.87587162790698, 2.30062255813953, 1.39104744186046, -0.367803255813954, 
0.141554418604649, -0.958681162790697, -0.735337441860466, 1.08400930232558
), T17 = c(1.08517051162791, 0.37491418604651, -9.69767441851843e-05, 
0.26748511627907, 1.06432162790697, 1.15751376744186, 0.136066744186047, 
0.932665813953487, 0.238209302325583, 0.470447906976744, 0.740903255813953, 
0.510037441860464, 1.60780860465116, 2.99557162790698, 0.600442558139534, 
0.541527441860466, 1.52836674418605, 1.30376441860465, -0.469871162790697, 
-1.04561744186047, -0.112610697674419), T16 = c(0.522980511627908, 
0.86899418604651, 0.226203023255815, 0.458105116279069, 1.06149162790697, 
0.964783767441861, 0.662886744186046, 0.845595813953487, 0.343449302325583, 
0.875097906976743, 0.477433255813954, 0.967387441860464, 2.45403860465116, 
1.80407162790698, 2.48462255813953, -0.341292558139535, 0.168956744186047, 
0.0499744186046485, -0.398191162790697, -0.906957441860466, -0.106630697674419
), T13 = c(0.0701405116279075, 0.59358418604651, 0.816473023255815, 
0.513745116279069, 0.413971627906975, 0.147633767441861, 0.331156744186046, 
0.542285813953487, 0.369419302325583, 0.459487906976744, 0.315323255813953, 
-0.0838525581395357, 3.08273860465116, 1.81467162790698, 1.46382255813953, 
0.309577441860465, 1.48057674418605, -0.196025581395351, -0.296261162790697, 
-0.461077441860466, 0.428129302325581), tP7 = c(0.170280511627907, 
0.46002418604651, -0.201286976744185, 0.362865116279069, 0.303761627906974, 
0.0906237674418611, 0.571676744186046, 0.392705813953487, 0.0534793023255826, 
0.140527906976743, 0.255693255813954, 0.505877441860465, 0.0213586046511622, 
0.459071627906981, -0.748247441860467, 0.635857441860465, 0.402586744186046, 
-1.19998558139535, -0.121731162790697, -1.34132744186047, -0.188270697674419
), tP9 = c(0.557250511627907, 1.00745418604651, 0.776833023255815, 
0.56926511627907, 0.800811627906975, 0.955983767441861, 0.783726744186046, 
0.689515813953487, 0.720239302325583, 0.745837906976743, 0.972283255813954, 
0.939047441860464, 0.533908604651161, 0.70187162790698, -0.367007441860467, 
-0.372862558139535, -0.203393255813953, -0.669325581395351, -0.415761162790697, 
-0.754107441860466, -0.549350697674419), tP4 = c(-0.232909488372093, 
0.11967418604651, -0.168156976744185, -0.246064883720931, 0.152971627906974, 
-0.0189962325581385, 0.332076744186046, 0.210345813953487, -0.280340697674418, 
0.357727906976743, 0.633703255813954, -0.127502558139535, -0.312461395348838, 
-1.28051837209302, -2.00501744186047, -0.624152558139535, -0.487683255813954, 
-0.862845581395352, -0.413171162790698, -0.203017441860466, -0.851130697674419
), tP1 = c(0.0387105116279072, 0.522514186046511, 0.244123023255815, 
-0.0110548837209308, 0.796201627906974, -0.154686232558139, 0.189146744186046, 
0.191425813953487, 0.215049302325583, 0.572767906976743, 0.080133255813954, 
-0.266962558139536, 1.36522860465116, 1.23807162790698, 0.644622558139533, 
0.364947441860465, -0.722553255813954, -1.70286558139535, -0.197961162790698, 
-1.19344744186047, -1.24844069767442), tP19 = c(-0.285929488372092, 
0.325744186046511, -0.133036976744185, 0.0324451162790691, 0.550341627906975, 
0.729833767441861, 0.603686744186046, 0.337385813953487, 0.530419302325583, 
0.305797906976744, 0.794433255813954, 0.711507441860465, 0.413408604651162, 
1.26297162790698, -0.496287441860467, -0.546992558139535, -1.07068325581395, 
-1.16344558139535, -0.283051162790697, -1.01281744186047, -0.104430697674419
), T18 = c(-0.117649488372093, 0.67459418604651, 0.282503023255815, 
0.811895116279069, 1.05705162790697, 0.230333767441861, 0.418676744186047, 
-0.0544241860465124, -0.0520706976744174, -1.09494209302326, 
-0.169736744186046, -0.277752558139535, -1.11457139534884, 0.273071627906981, 
-0.503727441860468, -1.30426255813954, -0.460323255813953, -0.464655581395352, 
-0.00711116279069746, -0.164367441860466, 0.925569302325581), 
    tP6 = c(0.329870511627907, 0.63120418604651, 0.682723023255815, 
    0.301385116279069, 1.07571162790697, 0.661583767441861, 0.566136744186046, 
    0.345795813953488, 0.392539302325583, 0.745847906976743, 
    0.230483255813954, 0.472967441860464, 0.198668604651161, 
    -1.70168837209302, -1.02288744186047, -0.726732558139535, 
    -0.110733255813954, -0.498835581395351, -0.199721162790698, 
    1.24939255813953, -0.838590697674419), tP11 = c(0.169250511627907, 
    0.78092418604651, 0.489233023255816, 0.172385116279069, 0.592081627906975, 
    0.549083767441862, 0.428546744186046, 0.314575813953487, 
    0.180189302325583, -0.0110720930232571, 0.300023255813954, 
    0.242637441860465, 0.531928604651162, 0.738771627906981, 
    -0.635907441860467, 0.145547441860464, -0.235943255813953, 
    -1.16997558139535, -0.437811162790697, -0.180037441860466, 
    -0.962710697674419), tP13 = c(1.20259051162791, 0.621614186046511, 
    0.576753023255815, 0.484445116279069, -0.280598372093025, 
    1.68878376744186, 1.12385674418605, 1.78414581395349, 1.30433930232558, 
    0.572757906976744, 1.08123325581395, 1.46034744186046, -0.700561395348839, 
    -0.203928372093019, 0.00155255813953303, -1.57593255813954, 
    -0.843253255813953, -0.974485581395352, -1.8441111627907, 
    -0.238897441860466, -1.22581069767442), tP2 = c(0.434700511627907, 
    0.776764186046511, 0.956083023255816, 0.79627511627907, 1.21089162790697, 
    0.896403767441861, 0.493776744186047, 0.475895813953487, 
    0.340049302325583, 0.904867906976743, 1.10308325581395, 0.757687441860464, 
    -1.15365139534884, -1.04773837209302, -0.487427441860467, 
    -0.969262558139535, -1.05171325581395, -0.353815581395351, 
    -0.508501162790697, -0.234267441860466, -1.14279069767442
    ), tP22 = c(0.685860511627907, 0.84772418604651, 0.880703023255816, 
    1.01833511627907, 1.36494162790697, 1.06924376744186, 1.12907674418605, 
    0.723095813953488, 0.658329302325583, 1.00074790697674, 0.709063255813954, 
    1.08472744186046, -1.95875139534884, -0.55582837209302, -1.24920744186047, 
    -1.21412255813954, -1.28597325581395, -0.205465581395352, 
    -0.201361162790697, 0.518552558139534, -0.984290697674419
    ), tP17 = c(0.662490511627907, 0.655254186046511, 0.508513023255815, 
    0.68800511627907, 1.20150162790697, 0.667983767441862, 0.0815467441860465, 
    0.621385813953487, 0.494099302325583, 1.00233790697674, 0.469543255813954, 
    0.661097441860464, -1.08645139534884, -0.050728372093019, 
    -0.146737441860466, -0.239302558139535, -0.735603255813953, 
    -0.355885581395351, -0.947971162790697, -0.273967441860466, 
    -1.05466069767442), tP16 = c(0.953560511627908, 0.90733418604651, 
    0.649283023255815, 0.460705116279069, 0.895531627906974, 
    1.13503376744186, 0.797226744186046, 0.996325813953487, 0.330869302325582, 
    0.733407906976743, 0.881003255813954, 0.720527441860464, 
    -0.574011395348839, -0.360328372093019, -0.679527441860467, 
    -1.59138255813953, -0.775713255813954, -0.349415581395352, 
    -0.920961162790698, 0.221462558139534, -0.213160697674419
    ), tP15 = c(0.636050511627907, 1.04358418604651, 0.311593023255815, 
    0.70217511627907, 0.0984216279069745, 1.28490376744186, 1.31144674418605, 
    1.03450581395349, 0.723999302325582, 0.369737906976743, 1.18222325581395, 
    1.34220744186046, 0.385668604651162, -0.78132837209302, 2.03482255813953, 
    -1.21390255813953, -0.0594532558139536, -0.759855581395351, 
    -1.0176411627907, -0.862457441860466, -0.248570697674419), 
    tP3 = c(0.490740511627907, 1.04797418604651, 0.972513023255815, 
    0.60267511627907, 1.00756162790697, 0.397903767441862, 0.729276744186047, 
    0.928365813953487, 0.436249302325582, 0.987387906976744, 
    0.478183255813954, 0.183087441860464, -1.62335139534884, 
    -1.47187837209302, -1.59381744186047, -1.22637255813954, 
    -0.899693255813953, -0.239585581395351, -0.541461162790697, 
    -0.846487441860466, -0.156480697674419), tP12 = c(0.397220511627907, 
    0.709334186046511, 0.481883023255815, 0.18157511627907, 0.810201627906975, 
    0.631033767441862, 0.874396744186046, 1.19741581395349, 0.308289302325583, 
    -0.0654920930232565, 0.868703255813954, 0.851837441860464, 
    -0.809981395348839, 0.527971627906981, -1.19100744186047, 
    -1.28037255813954, -1.02207325581395, -0.949775581395351, 
    -0.371941162790697, -0.197507441860466, -0.559550697674419
    ), tP23 = c(0.460680511627907, 0.69578418604651, 0.750913023255815, 
    1.17693511627907, 1.45920162790697, 0.763893767441862, 0.745066744186047, 
    0.909395813953488, 0.545619302325583, 1.07390790697674, 0.706933255813953, 
    0.720487441860465, -3.39339139534884, -2.38593837209302, 
    -2.01269744186047, -1.16821255813954, -1.37011325581395, 
    -0.832745581395351, -0.396931162790697, -0.124667441860466, 
    -0.821030697674419), tP5 = c(0.715760511627908, 1.31015418604651, 
    1.24940302325582, 0.671385116279069, 1.89191162790697, 0.822933767441861, 
    1.21669674418605, 0.891815813953487, 1.09032930232558, 1.34050790697674, 
    0.519283255813954, 0.434807441860465, -2.33249139534884, 
    -1.63118837209302, -1.58745744186047, -0.684022558139535, 
    -0.712933255813954, -0.475125581395352, -0.690841162790697, 
    -0.601797441860466, -0.0977606976744188), tP10 = c(1.37902051162791, 
    1.59641418604651, 1.11597302325582, 1.25279511627907, 1.86056162790697, 
    1.46124376744186, 1.42992674418605, 1.26051581395349, 1.02432930232558, 
    1.22563790697674, 1.12530325581395, 1.40960744186046, -3.61922139534884, 
    -1.57887837209302, -1.14383744186047, -0.933552558139535, 
    -0.737863255813953, -0.354035581395351, -0.563211162790697, 
    -0.222207441860466, -0.426710697674419), tP8 = c(0.833080511627907, 
    1.48323418604651, 1.39634302325581, 0.94987511627907, 1.77465162790697, 
    0.969653767441861, 1.11747674418605, 1.23117581395349, 1.23995930232558, 
    1.06780790697674, 0.680063255813954, 0.955387441860465, -3.62866139534884, 
    -1.88035837209302, -0.721067441860467, -0.478782558139535, 
    -0.969693255813954, -0.034075581395351, -0.712571162790697, 
    0.0778125581395344, -0.590640697674419), tP14 = c(0.573910511627907, 
    1.65765418604651, 1.26237302325582, 1.75955511627907, 1.42193162790697, 
    1.79397376744186, 1.08847674418605, 1.50737581395349, 1.47482930232558, 
    1.09951790697674, 1.09119325581395, 1.59916744186046, -4.21754139534884, 
    -2.26154837209302, -1.91009744186047, -2.43444255813954, 
    -1.35212325581395, -0.00481558139535121, -1.5644911627907, 
    0.622332558139534, -1.06022069767442), tP18 = c(1.07039051162791, 
    0.85198418604651, 2.07258302325581, 1.60016511627907, 2.39455162790698, 
    1.70859376744186, 1.70625674418605, 1.57102581395349, 1.74329930232558, 
    1.19263790697674, 0.925193255813954, 2.01881744186046, -3.57422139534884, 
    -1.78689837209302, -1.45942744186047, -1.53125255813953, 
    0.0985567441860464, -1.01357558139535, -0.435741162790698, 
    0.202642558139534, -1.19451069767442), tP20 = c(1.37944051162791, 
    1.47128418604651, 1.25694302325581, 0.94410511627907, 1.78154162790697, 
    1.57768376744186, 1.50204674418605, 1.41639581395349, 1.15531930232558, 
    1.43588790697674, 1.20766325581395, 1.36143744186046, -2.79161139534884, 
    -2.76455837209302, -1.58463744186047, -0.753742558139535, 
    -0.833903255813953, -0.271225581395352, -0.791951162790697, 
    -0.131877441860466, -0.768260697674419), tP21 = c(1.36801051162791, 
    1.62014418604651, 1.32456302325582, 1.42038511627907, 2.10722162790697, 
    1.91875376744186, 1.87060674418605, 1.50700581395349, 1.46246930232558, 
    1.40279790697674, 1.37821325581395, 1.81393744186046, -2.97174139534884, 
    -2.34306837209302, -0.586297441860467, -1.06734255813953, 
    -0.655663255813954, -0.218455581395351, -1.2255811627907, 
    -0.00223744186046559, -0.332270697674419), Cluster = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L)), row.names = 50:70, class = "data.frame")
> 

What I tried

m <- as.matrix(d[, -c(1, ncol(d)) ])
rownames(m) <- d$Gene.Name
heatmap(m)

Unfortunately the produced heatmap does not overlap with the expected result.

Using "cluster_columns = FALSE" didn't work either.


Solution

  • rownames(df) <- df[,1]
    
    df <- df[,-c(1, 45)]
    
    cols <- colorRampPalette(c("dodgerblue1", "white", "orange2"))(35)
    

    with gplots::heatmap.2

    gplots::heatmap.2(as.matrix(df), Rowv=NA, Colv=NA, 
                        dendrogram="none", trace="none", col=cols)
    

    gplots::heatmap.2

    with base heatmap

    heatmap(as.matrix(df[nrow(df):1,]), Rowv=NA, Colv=NA, scale="none", col=cols)
    

    base::heatmap