How to color the branches and tick labels in the heatmap.2?

不羁岁月 提交于 2019-12-30 06:35:14

问题


I have done a Heat Map using the function heatmap.2 of gplots in R, but I don't have an idea of how to coloring the branches and tick labels per groups (Eg. if I cut the tree to have four gruops like in my second figure). I have checked that it is possible to color the dendrogram alone using dendextend package.

Also there is a heatmap here: selecting number of leaf nodes of dendrogram in heatmap.2 in R with a colored dendrogram, but I can't implement it in my example.

Somebody can help me with this issue?

Update

This is my Heat Map:

and I would like to have one like this with branches and tick labels in color according their four groups (this figure was edited with Illustrator to explain this question):

Here is the data and code that I have used:

Data

YEAR    varA    varB    varC    varD    varE    varF    var1    var2    var3    var4    var5    var6    var7    var8    var9    var10   var11   var12   var13   var14   var15   var16   var17
2005    1.175290887 1.535846033 1.531113178 -1.10297075 0.0284  26  -25.5470    -24.2101    24.7900 3.3345  0.0468  0.5058  0.0087  1.7378  0.0703  2.7070  0.0183  0.0340  0.0177  0.0176  0.0240  0.0015  0.0292
2004    0.834733204 0.64917365  -0.403174087    0.116169692 0.033   50  -24.4170    -22.2574    27.3400 3.4106  0.1151  0.5822  0.0085  1.8133  0.0762  3.2604  0.0114  0.0178  0.0086  0.0086  0.0824  0.0018  0.0308
2003    1.297607635 1.224946337 0.4486378   0.227557968 0.0544  181 -24.5080    -23.2790    27.4200 3.5092  0.1052  0.5239  0.0038  0.9815  0.0681  2.7465  0.0074  0.0099  0.0025  0.0025  0.0142  0.0015  0.0298
2002    1.043780072 0.650695815 -0.337133061    0.016766696 0.0374  227 -22.6110    -21.7828    30.0200 3.6270  0.1119  0.5753  0.0106  0.7916  0.0805  3.0434  0.0069  0.0086  0.0109  0.0108  0.0313  0.0017  0.0288
2001    0.781864124 0.534881678 -0.740527443    0.171745261 0.0074  20  -23.9170    -23.2327        3.8007  0.1243  0.6216  0.0553  1.2333  0.3414  2.9606  0.0074  0.0384  0.0079  0.0082  0.0570  0.0018  0.0360
2000    0.742528229 0.667207042 -0.614740091    0.189253192 0.0257  88  -22.6420    -21.4066    30.8900 3.1693  0.0287  0.6244  0.0070  1.0256  0.1336  2.7033  0.0063  0.0102  0.0185  0.0186  0.0248  0.0015  0.0278
1999    0.701222612 1.059869033 0.772334853 0.290190993 0.0476  312 -22.4730    -21.8328    26.6600 3.0578  0.0719  0.6363  0.0032  0.7183  0.0649  2.5445  0.0066  0.0070  0.0063  0.0063  0.0095  0.0016  0.0252
1998    0.904634938 1.16455833  0.646654191 0.086214161 0.0546  332 -23.2070    -22.4399    26.1400 3.2344  0.0656  0.7096  0.0046  0.6709  0.0718  2.5656  0.0072  0.0166  0.0132  0.0131  0.0144  0.0016  0.0275
1997    0.965775183 1.362520795 0.653268963 0.007038426 0.0791  509 -23.4830    -22.4253    26.0400 3.0278  0.0438  0.7575  0.0081  0.5002  0.0657  2.5755  0.0077  0.0072  0.0083  0.0083  0.0108  0.0017  0.0252
1996    0.956113049 1.439534042 0.618648101 -0.334351083    0.0411  245 -23.4290    -23.0417    27.3000 2.9331  0.0363  0.9229  0.0050  0.4819  0.1306  2.7239  0.0072  0.0166  0.0027  0.0026  0.0174  0.0018  0.0240
1995    1.786742729 1.732091021 2.654237394 0.190377371 0.0842  646 -22.7600    -22.0212    24.2100 3.1562  0.0202  1.1728  0.0072  0.6133  0.0772  3.1313  0.0080  0.0051  0.0035  0.0035  0.0055  0.0022  0.0266
1994    0.811695681 0.670904284 0.76646691  2.163378723 0.0394  203 -22.4920    -21.3677    28.6500 3.2475  0.0132  1.7476  0.0084  0.9386  0.1880  3.8856  0.0082  0.0120  0.0129  0.0129  0.0151  0.0026  0.0280
1993    0.754876913 0.302624208 -0.927234708    -0.108263802    0.017   66  -22.3880    -21.2900    32.8400 3.5853  0.0008  1.6626  0.0221  1.2307  0.4173  3.8864  0.0079  0.0379  0.0199  0.0196  0.0225  0.0028  0.0319
1992    0.723058507 0.818965047 -0.52053294 0.384656566 0.0345  155 -21.4920    -20.8724    32.2000 3.3116  0.0068  1.5673  0.0104  0.9411  0.2245  4.0228  0.0075  0.0123  0.0308  0.0306  0.0112  0.0027  0.0287
1991    1.024225427 0.71537408  0.22672288  -0.029575009    0.0297  235 -23.4850    -22.7000    27.8400 4.4024  0.0097  1.6126  0.0698  0.6344  0.2832  4.4160  0.0108  0.0127  0.0184  0.0184  0.0122  0.0030  0.0356
1990    0.873807193 1.168599747 1.317306687 -0.335682786    0.0533  172 -23.7170    -23.5029    25.8100 4.0497  0.0170  1.5207  0.0065  0.5232  0.1734  4.6765  0.0104  0.0164  0.0131  0.0130  0.0093  0.0030  0.0332
1989    0.71498833  1.065965836 0.650281646 -0.048038841    0.0663  214 -23.5000    -23.1053    26.3500 4.1139  0.0159  1.6162  0.0096  0.5199  0.1426  4.7752  0.0106  0.0083  0.0098  0.0099  0.0076  0.0031  0.0341
1988    1.188282778 1.133076429 -0.167816244    0.448030288 0.007   64  -23.3750    -21.9900    29.3900 3.6893  0.0278  1.8392  0.0939  0.5658  1.2390  5.1103  0.0086  0.0775  0.0203  0.0202  0.0339  0.0034  0.0340
1987    0.788798159 0.276008942 -0.934596308    -0.039259431    0.012   65  -22.9540    -22.7758    28.3800 3.6375  0.0011  1.8331  0.0768  0.6187  0.6081  5.0475  0.0088  0.0554  0.0183  0.0180  0.0159  0.0038  0.0381
1986    0.757757883 1.395817348 0.455252572 -0.001274532    0.0125  47  -22.6120    -22.9011    29.7400 3.7060  0.0172  1.5279  0.0151  0.5897  0.6168  4.4917  0.0085  0.0160  0.0257  0.0256  0.0276  0.0033  0.0410
1985    1.128413419 0.321849225 -0.904189697    -0.05362552 0.0705  291 -22.7200    -21.9357    28.4100 3.5887  0.0100  1.4955  0.0022  0.3538  0.1471  4.3125  0.0091  0.0157  0.0042  0.0042  0.0041  0.0029  0.0292
1984    1.015352865 1.014625668 0.39294569  -0.267936245    0.0419  121 -23.5170    -23.1678    25.6200 4.5018  0.0353  1.8985  0.0022  0.3420  0.2620  4.9867  0.0113  0.0069  0.0058  0.0058  0.0051  0.0033  0.0356
1983    0.393985784 0.474743555 -0.368393191    -0.222845745    0.0161  49  -24.5600    -23.9514    30.5300 3.0978  0.0270  0.9467  0.0421  0.3287  0.5616  3.1256  0.0075  0.0553  0.0154  0.0155  0.0084  0.0022  0.0323
1982    0.503744901 0.524683063 -0.946225504    0.016766696 0.0118  10  -23.5970    -24.0037    30.3100 2.7288  0.0011  1.2154  0.0097  0.3022  0.8415  4.3594  0.0083  0.0254  0.0075  0.0076  0.0134  0.0029  0.0304
1981    0.872025585 1.496555573 0.658923526 -0.175816424    0.0489  343 -23.8320    -23.4716    28.1100 4.6585  0.0128  1.9205  0.0031  0.2999  0.2278  5.6588  0.0134  0.0067  0.0072  0.0071  0.0087  0.0036  0.0437
1980    2.165460373 3.419095697 3.741300435 0.250364758 0.0644  626 -24.5010    -24.0323    28.7300 3.8474  0.0122  1.4827  0.0019  0.2164  0.1859  4.3602  0.0104  0.0056  0.0050  0.0050  0.0064  0.0028  0.0337
1979    1.00201444  0.453601121 0.109577407 0.73158507  0.0281  301 -23.6070    -22.9149    27.9100 4.5765  0.0467  1.6919  0.0344  0.1940  0.3453  5.1064  0.0132  0.0162  0.0078  0.0077  0.0554  0.0032  0.0389
1978    0.829984787 0.2021646   -0.724630653    -0.178430782                                            0.0000                              
1977    0.939170906 0.192142351 -1.029656979    0.50745842  0.0068  30  -24.3510    -22.5760    29.4900 6.1029  0.3417  2.4069  0.0938  0.2824  1.3937  6.6441  0.0136  0.0609  0.0395  0.0391  0.6074  0.0045  0.0591
1976    0.741090851 0.151474404 -0.439448642    0.359471579 0.056   396 -23.7450    -22.7680    28.3700 4.3464  0.0431  1.6901  0.0234  0.2937  0.2160  5.1366  0.0113  0.0147  0.0082  0.0081  0.0317  0.0034  0.0389
1975    1.061884929 0.396763153 -1.075320241    0.433356946 0.0299  322 -23.4320    -22.9732    25.7800 5.0301  0.1740  2.2028  0.0311  0.3131  0.4254  5.8683  0.0131  0.0160  0.0182  0.0182  0.2093  0.0038  0.0443
1974    1.052548763 0.491883924 0.28198823  -0.562241025    0.0215  267 -23.3350    -22.7075    26.4100 5.3407  0.1187  2.2436  0.0231  0.2984  0.5378  5.8795  0.0127  0.0208  0.0127  0.0128  0.0821  0.0038  0.0466
1973    0.519163031 1.120525721 0.960322396 -0.84893256 0.0129  49  -23.4350    -23.0556    31.3500 6.4341  0.1105  2.4298  0.0484  0.2783  0.9249  5.8779  0.0129  0.0428  0.0124  0.0123  0.1293  0.0038  0.0499
1972    0.703961551 1.359485416 -0.306513069    -1.150818704    0.0228  247 -23.7840    -23.3257    28.3000 6.3520  0.1096  2.6043  0.0439  0.4126  0.5335  6.3320  0.0154  0.0279  0.0061  0.0062  0.0874  0.0042  0.0593
1971    0.714252707 1.621333793 -1.065184704    0.003023451 0.0274  196 -23.2140    -22.2731    31.3800 5.1332  0.0873  1.9259  0.0872  0.3598  0.4714  4.9337  0.0112  0.0234  0.0073  0.0073  0.0688  0.0034  0.0426
1970    1.022643019 1.491401283 0.088239434 -0.973528472    0.025   206 -22.9870    -21.9506    30.6200 5.0770  0.0698  2.1145  0.1825  0.3537  0.4990  5.3274  0.0129  0.0873  0.0098  0.0098  0.0316  0.0040  0.0479
1969    2.157784838 1.796722133 0.731152565 -0.193891705    0.0547  505 -24.2820    -23.9048    26.2400 5.0183  0.0637  2.2673  0.0127  0.2893  0.2420  5.1038  0.0129  0.0244  0.0069  0.0069  0.0154  0.0037  0.0440
1968    0.913026742 1.271215847 0.196849717 -1.068149218    0.0132  112 -22.9850    -21.9397    32.2300 4.0568  0.0498  2.0576  0.0965  0.2188  0.9468  5.3597  0.0080  0.0513  0.0157  0.0154  0.0507  0.0039  0.0371
1967    0.749350643 0.439194622 -1.316546028    0.306149455 0.0209  196 -23.7020    -22.8580    30.5400 4.5873  0.0703  1.9639  0.4981  0.2136  0.6086  5.1528  0.0100  0.0934  0.0103  0.0102  0.0235  0.0042  0.0415
1966    0.732785384 0.74795644  -0.681581292    1.265096245 0.0189  204 -23.3746    -22.7452    30.0600 4.8598  0.0542  1.8172  0.0437  0.2605  0.6557  5.2782  0.0131  0.0118  0.0081  0.0080  0.0203  0.0036  0.0418
1965    0.613725701 0.507953446 -1.91048851 0.825418348 0.0073  75  -24.2131    -22.5251    30.1900 5.5445  0.0691  1.9367  0.9303  0.2240  1.6461  5.5971  0.0119  0.1519  0.0318  0.0322  0.0436  0.0053  0.0467
1964    0.761469549 0.591007527 -0.715988774    -0.038091331                                            0.0000                              
1963    0.863218851 0.888615198 -0.331691877    -0.251436807    0.0123  121 -25.0690    -24.5964    27.4600 6.3232  0.0777  2.0383  0.1999  0.2465  0.9724  5.8291  0.0133  0.0349  0.0130  0.0131  0.0240  0.0044  0.0519
1962    1.194332086 1.123299319 1.400311402 -0.006545299    0.0296  250 -23.6850    -23.4588    29.3800 5.7280  0.0771  1.8900  0.0077  0.1952  0.4429  5.7635  0.0122  0.0047  0.0064  0.0063  0.0121  0.0041  0.0471
1961    0.685968021 0.396586649 -0.75076967     0.0168  201 -26.3352    -26.3457        5.5119  0.0726  1.9270  0.0180  0.1741  0.7887  5.7523  0.0121  0.0080  0.0119  0.0119  0.0208  0.0043  0.0496
1960    0.881343621 0.681729796 -0.466014418        0.0242  250 -25.5025    -25.2769    29.1200 6.5630  0.1133  2.2199  0.1176  0.2603  0.5894  6.4430  0.0159  0.0392  0.0062  0.0061  0.0308  0.0051  0.0647
1959    0.976463783 0.856497076 -0.769653776        0.0046  109 -24.9889    -25.0234    28.1000 7.4239  0.0760  3.3692  3.7315  0.4288  2.8041  7.8173  0.0178  0.6213  0.0559  0.0554  0.0902  0.0115  0.0722
1958    1.267054108 0.846073161 -0.698278256        0.0069  41  -24.5183    -25.8900    24.7200 8.4312  0.0602  3.1824  0.6086  0.4111  1.6313  7.3141  0.0165  0.0977  0.0280  0.0279  0.0575  0.0046  0.0709
1957    0.811849325 0.818326511 -1.087269506        0.0126  95  -23.4967    -23.5870    32.4900 5.6488  0.0761  2.6156  0.2207  0.4425  1.0305  7.3572  0.0159  0.0726  0.0380  0.0377  0.0437  0.0059  0.0573
1956    0.837065839 1.0007592   0.424525891     0.0115  76  -23.4403    -22.9419    32.1500 5.6087  0.0844  2.8347  0.3853  0.3125  1.1162  8.0455  0.0167  0.0696  0.0158  0.0157  0.0306  0.0058  0.0565
1955    2.044375189 1.828578166         0.0218  128 -24.9729    -24.2108    26.9000 7.4702  0.1659  4.0858  0.2619  0.3952  0.7023  9.7602  0.0222  0.0635  0.0111  0.0111  0.0338  0.0070  0.0731
1954    0.737033129 1.060103924         0.0029  8   -25.6604    -25.1068    28.9700 7.8034  0.0884  4.0907  1.8003  0.4834  5.0243  8.9409  0.0243  0.4037  0.0541  0.0529  0.2932  0.0091  0.0813
1953    0.619590578 0.647436408         0.0075  109         31.0400                                                     
1952    0.671851137 1.325676852         0.00562 41          33.1100                                                     
1951    0.894632264 1.397998867         0.00374 95          35.1800                                                     
1950    0.793048089 0.55195169          0.00186 76  -24.6750    -24.0405    37.2500 6.8214  0.1632  3.3876  1.0452  0.4622  1.7704  7.9556  0.0223  0.2316  0.0594  0.0592  0.3935  0.0066  0.0673
1949    0.70029018  1.053010492         0.0061  23  -25.2148    -26.0272    31.0900 5.8770  0.0532  3.0895  0.1231  0.4304  2.1365  7.9355  0.0165  0.1047  0.0204  0.0201  0.0735  0.0060  0.0578
1948    1.051413064 0.611568416         0.0105  86  -25.9116    -25.3761    29.6500 4.0905  0.0930  2.3578  0.7431  0.1757  1.3103  7.2889  0.0122  0.1378  0.0138  0.0136  0.0408  0.0056  0.0441
1947    0.706745895 0.323498221         0.0108  129 -26.5485    -25.8733    29.7700 5.7245  0.1294  3.2072  0.0524  0.2021  1.2550  9.1257  0.0150  0.1170  0.0155  0.0155  0.0393  0.0060  0.0588
1946    1.550656194 1.598435187         0.0164  381 -27.4603    -26.6368    28.0600 5.8659  0.1405  2.7682  0.0353  0.2424  0.3504  8.4089  0.0130  0.0437  0.0075  0.0075  0.0176  0.0057  0.0516
1945    0.877065687 0.539494611         0.0199  169 -26.7543    -26.0271    24.5700 6.2789  0.1407  2.9213  0.0309  0.3404  0.2888  7.9661  0.0131  0.0460  0.0079  0.0079  0.0185  0.0054  0.0507
1944    0.630508563 0.833959181         0.0116  20  -26.8748    -25.0203    29.4600 7.8427  0.0963  3.3664  0.8484  0.4187  0.4954  6.6868  0.0172  0.1799  0.0114  0.0114  0.0185  0.0066  0.0697
1943    0.948762137 0.552892235         0.0392  309 -24.8697    -26.9799    24.9700 7.2577  0.1020  3.2354  0.1611  0.3774  0.7706  8.0918  0.0196  0.0457  0.0060  0.0060  0.0120  0.0055  0.0699
1942    0.950673449 1.135547963         0.0148  18  -22.5094    -22.8155    28.5600 7.6926  0.1348  3.3979  0.6492  0.3347  1.3499  8.7744  0.0190  0.1142  0.0095  0.0095  0.0208  0.0072  0.0710
1941    1.185071356 1.263733805         0.0107  10  -24.3510    -22.5329    29.8200 6.2710  0.1459  3.3306  0.0560  0.3519  1.0068  9.4886  0.0179  0.0185  0.0196  0.0198  0.1190  0.0066  0.0613
1940    1.262322422 0.924262914         0.0168  133 -25.2962    -25.0828    26.2600 7.9568  0.1977  3.2329  0.0803  0.3561  3.2999  9.5743  0.0200  0.0232  0.0125  0.0125  0.0538  0.0065  0.0702
1939    1.114823086 1.548939022         0.0158  25  -25.5439    -24.3820    27.9800 4.2674  0.1624  2.3578  0.4553  0.3042  2.2656  7.3905  0.0087  0.0741  0.0100  0.0100  0.3075  0.0059  0.0413
1938    0.639727143 0.569847918         0.0115  5   -23.4696    -22.7480        5.0000  0.0751  2.6663  0.4021  0.2049  0.4997  7.9594  0.0121  0.0753  0.0093  0.0092  0.0819  0.0068  0.0485
1937    0.844930794 1.201811673         0.0269  13  -24.2616    -24.5915    25.9500 4.5623  0.0912  2.3393  0.0227  0.3172  0.2136  7.5512  0.0108  0.0093  0.0080  0.0079  0.1586  0.0049  0.0397
1936    0.603048989 0.528796963         0.0167  4   -23.4819    -23.1849    29.0200 7.1722  0.0600  2.7679  0.0126  0.2080  1.1025  7.5967  0.0175  0.0076  0.0094  0.0095  0.0608  0.0052  0.0569
1935    0.739921482 0.980951812         0.0369  402 -25.3542    -25.7692    30.5500 4.8218  0.0563  2.1489  0.0084  0.2337  1.3120  6.8994  0.0154  0.0044  0.0081  0.0081  0.0329  0.0047  0.0404
1934    0.936808475 1.350050919         0.0289  166 -26.1766    -24.8557    26.5700 4.2794  0.0626  2.1503  0.0112  0.3330  1.5501  6.8375  0.0072  0.0045  0.0248  0.0249  0.0818  0.0046  0.0362
1933    0.822006233 0.980858486         0.0187  215 -25.2825    -24.7483    27.0600 4.0682  0.0719  2.1376  0.0170  0.3042  3.6465  6.7130  0.0085  0.0074  0.0071  0.0071  0.0790  0.0047  0.0380
1932    1.128679304 1.122260931         0.0302  318 -26.5160    -24.7148    29.8100 3.4429  0.0475  2.1194  0.0111  0.2919  2.6147  7.5700  0.0093  0.0039  0.0069  0.0071  0.0472  0.0047  0.0336
1931    1.013960586 0.485124456         0.0189  13  -24.7074    -24.9517    30.7100 3.9828  0.0677  2.2806  0.0183  0.2268  3.7269  9.1548  0.0074  0.0089  0.0073  0.0073  0.0687  0.0057  0.0383
1930    1.148649752 1.029163891         0.0203  175 -26.8323    -26.0809    29.1800 3.0899  0.0697  3.5321  0.0158  0.3735  1.8765  13.0435 0.0121  0.0145  0.0103  0.0104  0.0397  0.0086  0.0506
1929    0.99387758  1.204846613         0.0376  104 -26.6411    -26.0890    28.1500 4.2733  0.0412  2.6675  0.0078  0.2893  0.1528  9.4824  0.0094  0.0112  0.0075  0.0075  0.0083  0.0064  0.0354
1928    0.905609551 0.772378969         0.0331  233 -25.8461    -26.2246    32.3600 5.8361  0.0440  2.8293  0.0095  0.2231  0.1736  8.7255  0.0186  0.0087  0.0074  0.0075  0.0091  0.0063  0.0476
1927    0.85672722  0.215215241         0.0171  152 -25.9555    -25.9299    28.1500 8.1915  0.1054  2.9585  0.0298  0.2692  0.3361  7.8459  0.0158  0.0135  0.0113  0.0112  0.2221  0.0057  0.0717
1926    0.932350398 0.425876672         0.0165  132 -27.7161    -26.9161    22.1900 7.5864  0.0875  3.2115  0.0256  0.2381  0.3483  8.4859  0.0152  0.0123  0.0127  0.0127  0.1256  0.0061  0.0618
1925    0.809324244 0.603492919         0.0174  48  -24.5765    -24.8562    28.9600 6.3520  0.0226  2.7524  0.0175  0.2355  0.3303  7.8838  0.0120  0.0130  0.0096  0.0096  0.0174  0.0058  0.0534
1924    1.735408827 1.991986688         0.027   253 -25.9985    -24.8571    31.4900 6.1000  0.1097  2.6762  0.0284  0.2676  2.2755  7.9132  0.0158  0.0089  0.0107  0.0106  0.2161  0.0054  0.0668
1923    0.787925712 1.573404755         0.0203  150 -24.6288    -25.1568    29.9300 5.6860  0.0967  2.5993  0.0231  0.2137  3.8395  9.0800  0.0128  0.0101  0.0098  0.0098  0.1010  0.0060  0.0536
1922    0.799163043             0.0208  334 -24.4215    -24.3729    28.8900 5.3341  0.0924  2.6394  0.0133  0.2462  3.8226  7.8138  0.0114  0.0069  0.0149  0.0150  0.0729  0.0054  0.0497
1921    0.77243578              0.0226  443 -23.4421    -23.8877    29.4300 6.1139  0.0805  3.2761  0.0156  0.2522  4.2754  10.1551 0.0128  0.0040  0.0195  0.0197  0.1065  0.0067  0.0623
1920    0.787155209             0.0385  278 -24.2587    -23.9798    29.2400 5.9896  0.0727  3.0804  0.0110  0.2266  3.7709  9.9680  0.0133  0.0038  0.0268  0.0269  0.0544  0.0067  0.0567
1919    0.836725864             0.0276  341 -24.7950    -24.8537    27.3900 6.5779  0.0798  3.1646  0.0126  0.2276  4.7733  10.8125 0.0149  0.0052  0.0154  0.0154  0.0604  0.0073  0.0629
1918    0.838156697             0.0058  392 -25.9260    -24.5236    30.6200 6.0259  0.0939  3.5283  0.0448  0.4603  6.5956  12.5834 0.0114  0.0238  0.0598  0.0605  0.2763  0.0095  0.0823
1917    0.966249549             0.0208  58  -25.5352    -24.7604    28.3400 5.8498  0.0925  2.8573  0.0143  0.2275  3.3143  9.2387  0.0118  0.0090  0.0238  0.0239  0.0445  0.0065  0.0535
1916    1.352618036             0.0152  567 -24.0530    -23.6626    27.6400 6.3964  0.0549  3.1876  0.0166  0.2559  6.1909  11.3232 0.0119  0.0088  0.0303  0.0302  0.0696  0.0078  0.0620
1915    0.56838431              0.0354  153 -23.6817    -23.9420    29.7600 5.9449  0.0494  3.1254  0.0118  0.2632  3.6600  10.8684 0.0125  0.0096  0.0234  0.0234  0.0455  0.0075  0.0580
1914    1.653698335             0.0096  355 -25.3230    -25.5543    30.4100 6.1042  0.0305  3.3067  0.0310  0.3592  11.7772 11.9468 0.0103  0.0189  0.0230  0.0230  0.0825  0.0083  0.0603
1913    0.673176646             0.018   479 -25.2734    -25.9128    31.0800 6.1167  0.1001  3.5575  0.0227  0.3392  8.3156  12.0722 0.0131  0.0069  0.0294  0.0291  0.0844  0.0083  0.0681
1912    1.168563731             0.0026  57  -25.4911    -25.0984    30.9900 8.2413  0.1793  5.4744  0.1320  0.7542  53.7132 17.0050 0.0120  0.1196  0.0562  0.0570  0.3436  0.0120  0.1118
1911    1.458277945             0.0119  43  -25.0742    -25.1744    29.2000 8.5525  0.0326  4.2884  0.0276  0.4920  13.5179 14.3376 0.0117  0.0126  0.0152  0.0153  0.0453  0.0096  0.0817
1910    1.653698335             0.0096  355 -25.3230    -25.5543    30.4100 6.1042  0.0305  3.3067  0.0310  0.3592  11.7772 11.9468 0.0103  0.0189  0.0230  0.0230  0.0825  0.0083  0.0603

Code

# reading data
test <- read.delim("clipboard", sep="")

rnames <- test[,1] 
test <- data.matrix(test[,2:ncol(test)]) # to matrix
rownames(test) <- rnames                 
test <- scale(test, center=T, scale=T) # data standarization
test <- t(test) # transpose


## Creating a color palette & color breaks

my_palette <- colorRampPalette(c("forestgreen", "yellow", "red"))(n = 299)

col_breaks = c(seq(-1,-0.5,length=100),  # forestgreen
               seq(-0.5,0.5,length=100), # yellow
               seq(0.5,1,length=100))    # red

# distance & hierarchical clustering
distance= dist(test, method ="euclidean")    
hcluster = hclust(distance, method ="ward.D")


# Creating Heat Map

  heatmap.2(test,  
            main = paste( "test"),  
            trace="none",          
            margins =c(5,7),      
            col=my_palette,        
            breaks=col_breaks,     
            dendrogram="row",      
            Rowv = as.dendrogram(hcluster),  
            Colv = "NA", 
            key.xlab = "Concentration (index)",
            cexRow =0.6,
            cexCol = 0.8,
            na.rm = TRUE ) 

回答1:


Solution: use the color_branches function from the dendextend package (or the set function, with the "branches_k_color", "k", and "value" parameters ).

First we need to get the data into R and create the relevant objects ready (this part is the same as the code in the question):

test <- read.delim("clipboard", sep="")

rnames <- test[,1] 
test <- data.matrix(test[,2:ncol(test)]) # to matrix
rownames(test) <- rnames                 
test <- scale(test, center=T, scale=T) # data standarization
test <- t(test) # transpose


## Creating a color palette & color breaks

my_palette <- colorRampPalette(c("forestgreen", "yellow", "red"))(n = 299)

col_breaks = c(seq(-1,-0.5,length=100),  # forestgreen
               seq(-0.5,0.5,length=100), # yellow
               seq(0.5,1,length=100))    # red

# distance & hierarchical clustering
distance= dist(test, method ="euclidean")    
hcluster = hclust(distance, method ="ward.D")

Next, we get the dendrogram and the heatmap ready:

dend1 <- as.dendrogram(hcluster)

# Get the dendextend package
if(!require(dendextend)) install.packages("dendextend")
library(dendextend)
# get some colors
cols_branches <- c("darkred", "forestgreen", "orange", "blue")
# Set the colors of 4 branches
dend1 <- color_branches(dend1, k = 4, col = cols_branches)
# or with:
# dend1 <- set(dend1, "branches_k_color", k = 4, value = cols_branches)

# get the colors of the tips of the dendrogram:
# col_labels <- cols_branches[cutree(dend1, k = 4)] # this may need tweaking in various cases - the following is a more general solution.

# The following code will work on its own once I uplode dendextend 0.18.6 to CRAN - but that can 
# take several good weeks until that happens. In the meantime
# Either use devtools::install_github('talgalili/dendextend')
# Or just the following:
source("https://raw.githubusercontent.com/talgalili/dendextend/master/R/attr_access.R")

col_labels <- get_leaves_branches_col(dend1)
# But due to the way heatmap.2 works - we need to fix it to be in the 
# order of the data!    
col_labels <- col_labels[order(order.dendrogram(dend1))]


# Creating Heat Map
if(!require(gplots)) install.packages("gplots")
library(gplots)
heatmap.2(test,  
        main = paste( "test"),  
        trace="none",          
        margins =c(5,7),      
        col=my_palette,        
        breaks=col_breaks,     
        dendrogram="row",      
        Rowv = dend1,  
        Colv = "NA", 
        key.xlab = "Concentration (index)",
        cexRow =0.6,
        cexCol = 0.8,
        na.rm = TRUE,
        RowSideColors = col_labels, # to add nice colored strips        
        colRow = col_labels # to add nice colored labels - only for qplots 2.17.0 and higher
        ) 

Which produces this plot:

For more details on the package, you can have a look at its vignette.

p.s.: to get the labels colored depends on parameters of heatmap.2, and this should be asked from the maintainer of gplots (i.e.: from greg at warnes.net)

update: this answer now includes the new "colRow" parameter in qplots 2.17.0.




回答2:


this is the maintainer of the gplots package.

I've added two new arguments to the gplots::heatmap.2 function, 'colRow' and 'colCol' to control the colors of the row and column labels. This will be part of gplots 2.17.0 which should be submitted to CRAN in the next day or so.



来源:https://stackoverflow.com/questions/29265536/how-to-color-the-branches-and-tick-labels-in-the-heatmap-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!