Given a matrix expressing the cross-similarity between two (possibly different) sets of entities, this uses slanted_orders to compute the "best" order for visualizing the matrix, then returns the reordered data. Commonly used in pheatmap(slanted_reorder(data), ...), and of course sheatmap does this internally for you.

slanted_reorder(
data,
order_data = NULL,
order_rows = TRUE,
order_cols = TRUE,
squared_order = TRUE,
same_order = FALSE,
discount_outliers = TRUE
)

## Arguments

data A rectangular matrix to reorder, of non-negative values (unless order_data is specified). An optional matrix of non-negative values of the same size to use for computing the orders. Whether to reorder the rows. Whether to reorder the columns. Whether to reorder to minimize the l2 norm (otherwise minimizes the l1 norm). Whether to apply the same order to both rows and columns. Whether to do a final order phase discounting outlier values far from the diagonal.

## Value

A matrix of the same shape whose rows and columns are a permutation of the input.

## Examples

slanter::slanted_reorder(cor(t(mtcars)))
#>                     Cadillac Fleetwood Pontiac Firebird Hornet 4 Drive
#> Cadillac Fleetwood           1.0000000        0.9996817      0.9978952
#> Pontiac Firebird             0.9996817        1.0000000      0.9990297
#> Hornet 4 Drive               0.9978952        0.9990297      1.0000000
#> Lincoln Continental          0.9995539        0.9992724      0.9970348
#> Merc 240D                    0.9881256        0.9911454      0.9959252
#> Dodge Challenger             0.9991617        0.9995087      0.9982656
#> Valiant                      0.9975630        0.9986500      0.9992580
#> Hornet Sportabout            0.9986132        0.9990524      0.9975793
#> AMC Javelin                  0.9983419        0.9987290      0.9972864
#> Chrysler Imperial            0.9969115        0.9969150      0.9942116
#> Merc 450SLC                  0.9828425        0.9833994      0.9800784
#> Merc 450SE                   0.9827991        0.9834429      0.9801772
#> Merc 450SL                   0.9827183        0.9834394      0.9802828
#> Duster 360                   0.9787143        0.9789599      0.9744818
#> Camaro Z28                   0.9756237        0.9758341      0.9710662
#> Mazda RX4                    0.9747543        0.9768412      0.9755949
#> Mazda RX4 Wag                0.9747020        0.9767863      0.9756154
#> Merc 230                     0.9713151        0.9741785      0.9754758
#> Ford Pantera L               0.9664936        0.9668629      0.9615811
#> Merc 280C                    0.9681688        0.9697178      0.9674808
#> Merc 280                     0.9679019        0.9696245      0.9675175
#> Porsche 914-2                0.9545629        0.9584635      0.9592908
#> Honda Civic                  0.9241250        0.9320459      0.9409279
#> Toyota Corona                0.9471343        0.9503172      0.9503239
#> Datsun 710                   0.9339623        0.9376925      0.9377512
#> Volvo 142E                   0.9297053        0.9326570      0.9309608
#> Fiat X1-9                    0.9158350        0.9223159      0.9273347
#> Fiat 128                     0.9012275        0.9089942      0.9157302
#> Maserati Bora                0.8880549        0.8885687      0.8802035
#> Toyota Corolla               0.8718034        0.8805717      0.8882435
#> Ferrari Dino                 0.8635937        0.8654714      0.8588699
#> Lotus Europa                 0.8515095        0.8566400      0.8552800
#>                     Lincoln Continental Merc 240D Dodge Challenger   Valiant
#> Cadillac Fleetwood            0.9995539 0.9881256        0.9991617 0.9975630
#> Pontiac Firebird              0.9992724 0.9911454        0.9995087 0.9986500
#> Hornet 4 Drive                0.9970348 0.9959252        0.9982656 0.9992580
#> Lincoln Continental           1.0000000 0.9868169        0.9996470 0.9977794
#> Merc 240D                     0.9868169 1.0000000        0.9902099 0.9948513
#> Dodge Challenger              0.9996470 0.9902099        1.0000000 0.9990828
#> Valiant                       0.9977794 0.9948513        0.9990828 1.0000000
#> Hornet Sportabout             0.9994983 0.9893532        0.9998857 0.9987885
#> AMC Javelin                   0.9993949 0.9891081        0.9998142 0.9988216
#> Chrysler Imperial             0.9987959 0.9839211        0.9987105 0.9966330
#> Merc 450SLC                   0.9878395 0.9702379        0.9884685 0.9864103
#> Merc 450SE                    0.9877938 0.9705086        0.9884838 0.9864589
#> Merc 450SL                    0.9877098 0.9708627        0.9884775 0.9865508
#> Duster 360                    0.9843942 0.9626936        0.9845757 0.9814781
#> Camaro Z28                    0.9817339 0.9589152        0.9818811 0.9785863
#> Mazda RX4                     0.9803858 0.9709778        0.9826427 0.9826444
#> Mazda RX4 Wag                 0.9803290 0.9711244        0.9826074 0.9826990
#> Merc 230                      0.9765449 0.9756630        0.9799370 0.9825497
#> Ford Pantera L                0.9737102 0.9491148        0.9739763 0.9703559
#> Merc 280C                     0.9748547 0.9610683        0.9766813 0.9760419
#> Merc 280                      0.9745835 0.9614781        0.9765418 0.9759904
#> Porsche 914-2                 0.9615637 0.9603211        0.9656933 0.9679007
#> Honda Civic                   0.9290026 0.9585105        0.9375444 0.9470811
#> Toyota Corona                 0.9552364 0.9495368        0.9589193 0.9607729
#> Datsun 710                    0.9430615 0.9379252        0.9472385 0.9492576
#> Volvo 142E                    0.9396044 0.9278683        0.9429176 0.9433944
#> Fiat X1-9                     0.9241089 0.9381059        0.9312529 0.9378364
#> Fiat 128                      0.9094065 0.9305930        0.9177615 0.9258627
#> Maserati Bora                 0.9013784 0.8651658        0.9018176 0.8963508
#> Toyota Corolla                0.8812198 0.9061161        0.8906478 0.8998474
#> Ferrari Dino                  0.8781242 0.8486224        0.8801175 0.8765768
#> Lotus Europa                  0.8656163 0.8564798        0.8709701 0.8722962
#>                     Hornet Sportabout AMC Javelin Chrysler Imperial Merc 450SLC
#> Cadillac Fleetwood          0.9986132   0.9983419         0.9969115   0.9828425
#> Pontiac Firebird            0.9990524   0.9987290         0.9969150   0.9833994
#> Hornet 4 Drive              0.9975793   0.9972864         0.9942116   0.9800784
#> Lincoln Continental         0.9994983   0.9993949         0.9987959   0.9878395
#> Merc 240D                   0.9893532   0.9891081         0.9839211   0.9702379
#> Dodge Challenger            0.9998857   0.9998142         0.9987105   0.9884685
#> Valiant                     0.9987885   0.9988216         0.9966330   0.9864103
#> Hornet Sportabout           1.0000000   0.9999405         0.9992468   0.9903498
#> AMC Javelin                 0.9999405   1.0000000         0.9993749   0.9911796
#> Chrysler Imperial           0.9992468   0.9993749         1.0000000   0.9942034
#> Merc 450SLC                 0.9903498   0.9911796         0.9942034   1.0000000
#> Merc 450SE                  0.9903899   0.9911872         0.9941905   0.9999895
#> Merc 450SL                  0.9903916   0.9911818         0.9941296   0.9999749
#> Duster 360                  0.9868227   0.9876712         0.9917758   0.9995091
#> Camaro Z28                  0.9843162   0.9852514         0.9897951   0.9990266
#> Mazda RX4                   0.9849219   0.9858452         0.9883171   0.9976821
#> Mazda RX4 Wag               0.9848669   0.9858144         0.9882470   0.9976410
#> Merc 230                    0.9818801   0.9830387         0.9840321   0.9933019
#> Ford Pantera L              0.9769543   0.9780454         0.9836189   0.9968131
#> Merc 280C                   0.9792976   0.9805742         0.9842639   0.9971749
#> Merc 280                    0.9792033   0.9804235         0.9840579   0.9969982
#> Porsche 914-2               0.9687526   0.9698631         0.9723443   0.9880709
#> Honda Civic                 0.9398642   0.9406541         0.9378112   0.9501861
#> Toyota Corona               0.9622047   0.9638558         0.9674682   0.9870962
#> Datsun 710                  0.9510709   0.9528131         0.9571134   0.9806200
#> Volvo 142E                  0.9470799   0.9489501         0.9546893   0.9803080
#> Fiat X1-9                   0.9348882   0.9364749         0.9378163   0.9610464
#> Fiat 128                    0.9215490   0.9229437         0.9234681   0.9473084
#> Maserati Bora               0.9075443   0.9098086         0.9212426   0.9566258
#> Toyota Corolla              0.8950651   0.8966876         0.8976567   0.9269695
#> Ferrari Dino                0.8864176   0.8889901         0.9004177   0.9415899
#> Lotus Europa                0.8773760   0.8797277         0.8883790   0.9307268
#>                     Merc 450SE Merc 450SL Duster 360 Camaro Z28 Mazda RX4
#> Cadillac Fleetwood   0.9827991  0.9827183  0.9787143  0.9756237 0.9747543
#> Pontiac Firebird     0.9834429  0.9834394  0.9789599  0.9758341 0.9768412
#> Hornet 4 Drive       0.9801772  0.9802828  0.9744818  0.9710662 0.9755949
#> Lincoln Continental  0.9877938  0.9877098  0.9843942  0.9817339 0.9803858
#> Merc 240D            0.9705086  0.9708627  0.9626936  0.9589152 0.9709778
#> Dodge Challenger     0.9884838  0.9884775  0.9845757  0.9818811 0.9826427
#> Valiant              0.9864589  0.9865508  0.9814781  0.9785863 0.9826444
#> Hornet Sportabout    0.9903899  0.9903916  0.9868227  0.9843162 0.9849219
#> AMC Javelin          0.9911872  0.9911818  0.9876712  0.9852514 0.9858452
#> Chrysler Imperial    0.9941905  0.9941296  0.9917758  0.9897951 0.9883171
#> Merc 450SLC          0.9999895  0.9999749  0.9995091  0.9990266 0.9976821
#> Merc 450SE           1.0000000  0.9999947  0.9994897  0.9989992 0.9978407
#> Merc 450SL           0.9999947  1.0000000  0.9994309  0.9989318 0.9980218
#> Duster 360           0.9994897  0.9994309  1.0000000  0.9998887 0.9966708
#> Camaro Z28           0.9989992  0.9989318  0.9998887  1.0000000 0.9963801
#> Mazda RX4            0.9978407  0.9980218  0.9966708  0.9963801 1.0000000
#> Mazda RX4 Wag        0.9977928  0.9979733  0.9965779  0.9962848 0.9999946
#> Merc 230             0.9934467  0.9937331  0.9908191  0.9902667 0.9979862
#> Ford Pantera L       0.9968069  0.9967513  0.9985815  0.9992412 0.9952363
#> Merc 280C            0.9972201  0.9973257  0.9971237  0.9974082 0.9989810
#> Merc 280             0.9970806  0.9972066  0.9969199  0.9971885 0.9991635
#> Porsche 914-2        0.9884321  0.9888285  0.9870457  0.9873208 0.9960446
#> Honda Civic          0.9509727  0.9519069  0.9445592  0.9435349 0.9679038
#> Toyota Corona        0.9872684  0.9875682  0.9868730  0.9877597 0.9942831
#> Datsun 710           0.9808700  0.9812300  0.9810345  0.9823933 0.9897806
#> Volvo 142E           0.9804773  0.9807341  0.9819685  0.9837963 0.9879852
#> Fiat X1-9            0.9616029  0.9623204  0.9589004  0.9596667 0.9771175
#> Fiat 128             0.9480671  0.9489426  0.9445163  0.9451632 0.9665339
#> Maserati Bora        0.9565886  0.9565090  0.9634194  0.9672440 0.9598113
#> Toyota Corolla       0.9278273  0.9288236  0.9245797  0.9258214 0.9497922
#> Ferrari Dino         0.9416717  0.9417605  0.9483130  0.9526674 0.9495308
#> Lotus Europa         0.9311976  0.9316971  0.9350718  0.9389672 0.9467139
#>                     Mazda RX4 Wag  Merc 230 Ford Pantera L Merc 280C  Merc 280
#> Cadillac Fleetwood      0.9747020 0.9713151      0.9664936 0.9681688 0.9679019
#> Pontiac Firebird        0.9767863 0.9741785      0.9668629 0.9697178 0.9696245
#> Hornet 4 Drive          0.9756154 0.9754758      0.9615811 0.9674808 0.9675175
#> Lincoln Continental     0.9803290 0.9765449      0.9737102 0.9748547 0.9745835
#> Merc 240D               0.9711244 0.9756630      0.9491148 0.9610683 0.9614781
#> Dodge Challenger        0.9826074 0.9799370      0.9739763 0.9766813 0.9765418
#> Valiant                 0.9826990 0.9825497      0.9703559 0.9760419 0.9759904
#> Hornet Sportabout       0.9848669 0.9818801      0.9769543 0.9792976 0.9792033
#> AMC Javelin             0.9858144 0.9830387      0.9780454 0.9805742 0.9804235
#> Chrysler Imperial       0.9882470 0.9840321      0.9836189 0.9842639 0.9840579
#> Merc 450SLC             0.9976410 0.9933019      0.9968131 0.9971749 0.9969982
#> Merc 450SE              0.9977928 0.9934467      0.9968069 0.9972201 0.9970806
#> Merc 450SL              0.9979733 0.9937331      0.9967513 0.9973257 0.9972066
#> Duster 360              0.9965779 0.9908191      0.9985815 0.9971237 0.9969199
#> Camaro Z28              0.9962848 0.9902667      0.9992412 0.9974082 0.9971885
#> Mazda RX4               0.9999946 0.9979862      0.9952363 0.9989810 0.9991635
#> Mazda RX4 Wag           1.0000000 0.9981426      0.9951176 0.9990100 0.9991795
#> Merc 230                0.9981426 1.0000000      0.9885613 0.9964616 0.9966519
#> Ford Pantera L          0.9951176 0.9885613      1.0000000 0.9974507 0.9972767
#> Merc 280C               0.9990100 0.9964616      0.9974507 1.0000000 0.9999656
#> Merc 280                0.9991795 0.9966519      0.9972767 0.9999656 1.0000000
#> Porsche 914-2           0.9960766 0.9966691      0.9883380 0.9953131 0.9959150
#> Honda Civic             0.9681143 0.9778126      0.9425926 0.9617182 0.9632309
#> Toyota Corona           0.9944076 0.9950507      0.9898566 0.9960771 0.9963272
#> Datsun 710              0.9898806 0.9905132      0.9859917 0.9922630 0.9926709
#> Volvo 142E              0.9880478 0.9867305      0.9883243 0.9920354 0.9922724
#> Fiat X1-9               0.9773155 0.9834791      0.9627322 0.9765082 0.9775593
#> Fiat 128                0.9667258 0.9744802      0.9484588 0.9646508 0.9660922
#> Maserati Bora           0.9596644 0.9504740      0.9761991 0.9697838 0.9695713
#> Toyota Corolla          0.9500043 0.9589877      0.9310236 0.9487738 0.9504343
#> Ferrari Dino            0.9494473 0.9425465      0.9634519 0.9600815 0.9601352
#> Lotus Europa            0.9467045 0.9456460      0.9498219 0.9541565 0.9550090
#>                     Porsche 914-2 Honda Civic Toyota Corona Datsun 710
#> Cadillac Fleetwood      0.9545629   0.9241250     0.9471343  0.9339623
#> Pontiac Firebird        0.9584635   0.9320459     0.9503172  0.9376925
#> Hornet 4 Drive          0.9592908   0.9409279     0.9503239  0.9377512
#> Lincoln Continental     0.9615637   0.9290026     0.9552364  0.9430615
#> Merc 240D               0.9603211   0.9585105     0.9495368  0.9379252
#> Dodge Challenger        0.9656933   0.9375444     0.9589193  0.9472385
#> Valiant                 0.9679007   0.9470811     0.9607729  0.9492576
#> Hornet Sportabout       0.9687526   0.9398642     0.9622047  0.9510709
#> AMC Javelin             0.9698631   0.9406541     0.9638558  0.9528131
#> Chrysler Imperial       0.9723443   0.9378112     0.9674682  0.9571134
#> Merc 450SLC             0.9880709   0.9501861     0.9870962  0.9806200
#> Merc 450SE              0.9884321   0.9509727     0.9872684  0.9808700
#> Merc 450SL              0.9888285   0.9519069     0.9875682  0.9812300
#> Duster 360              0.9870457   0.9445592     0.9868730  0.9810345
#> Camaro Z28              0.9873208   0.9435349     0.9877597  0.9823933
#> Mazda RX4               0.9960446   0.9679038     0.9942831  0.9897806
#> Mazda RX4 Wag           0.9960766   0.9681143     0.9944076  0.9898806
#> Merc 230                0.9966691   0.9778126     0.9950507  0.9905132
#> Ford Pantera L          0.9883380   0.9425926     0.9898566  0.9859917
#> Merc 280C               0.9953131   0.9617182     0.9960771  0.9922630
#> Merc 280                0.9959150   0.9632309     0.9963272  0.9926709
#> Porsche 914-2           1.0000000   0.9815847     0.9981955  0.9968802
#> Honda Civic             0.9815847   1.0000000     0.9740879  0.9736937
#> Toyota Corona           0.9981955   0.9740879     1.0000000  0.9991120
#> Datsun 710              0.9968802   0.9736937     0.9991120  1.0000000
#> Volvo 142E              0.9939359   0.9630656     0.9978030  0.9990375
#> Fiat X1-9               0.9916326   0.9913067     0.9904406  0.9926894
#> Fiat 128                0.9847491   0.9934268     0.9818228  0.9851826
#> Maserati Bora           0.9633905   0.9048621     0.9730680  0.9768834
#> Toyota Corolla          0.9732722   0.9867701     0.9711836  0.9769965
#> Ferrari Dino            0.9594920   0.9089867     0.9697639  0.9761739
#> Lotus Europa            0.9660054   0.9397725     0.9725778  0.9811911
#>                     Volvo 142E Fiat X1-9  Fiat 128 Maserati Bora Toyota Corolla
#> Cadillac Fleetwood   0.9297053 0.9158350 0.9012275     0.8880549      0.8718034
#> Pontiac Firebird     0.9326570 0.9223159 0.9089942     0.8885687      0.8805717
#> Hornet 4 Drive       0.9309608 0.9273347 0.9157302     0.8802035      0.8882435
#> Lincoln Continental  0.9396044 0.9241089 0.9094065     0.9013784      0.8812198
#> Merc 240D            0.9278683 0.9381059 0.9305930     0.8651658      0.9061161
#> Dodge Challenger     0.9429176 0.9312529 0.9177615     0.9018176      0.8906478
#> Valiant              0.9433944 0.9378364 0.9258627     0.8963508      0.8998474
#> Hornet Sportabout    0.9470799 0.9348882 0.9215490     0.9075443      0.8950651
#> AMC Javelin          0.9489501 0.9364749 0.9229437     0.9098086      0.8966876
#> Chrysler Imperial    0.9546893 0.9378163 0.9234681     0.9212426      0.8976567
#> Merc 450SLC          0.9803080 0.9610464 0.9473084     0.9566258      0.9269695
#> Merc 450SE           0.9804773 0.9616029 0.9480671     0.9565886      0.9278273
#> Merc 450SL           0.9807341 0.9623204 0.9489426     0.9565090      0.9288236
#> Duster 360           0.9819685 0.9589004 0.9445163     0.9634194      0.9245797
#> Camaro Z28           0.9837963 0.9596667 0.9451632     0.9672440      0.9258214
#> Mazda RX4            0.9879852 0.9771175 0.9665339     0.9598113      0.9497922
#> Mazda RX4 Wag        0.9880478 0.9773155 0.9667258     0.9596644      0.9500043
#> Merc 230             0.9867305 0.9834791 0.9744802     0.9504740      0.9589877
#> Ford Pantera L       0.9883243 0.9627322 0.9484588     0.9761991      0.9310236
#> Merc 280C            0.9920354 0.9765082 0.9646508     0.9697838      0.9487738
#> Merc 280             0.9922724 0.9775593 0.9660922     0.9695713      0.9504343
#> Porsche 914-2        0.9939359 0.9916326 0.9847491     0.9633905      0.9732722
#> Honda Civic          0.9630656 0.9913067 0.9934268     0.9048621      0.9867701
#> Toyota Corona        0.9978030 0.9904406 0.9818228     0.9730680      0.9711836
#> Datsun 710           0.9990375 0.9926894 0.9851826     0.9768834      0.9769965
#> Volvo 142E           1.0000000 0.9871267 0.9780049     0.9851604      0.9697187
#> Fiat X1-9            0.9871267 1.0000000 0.9985037     0.9489268      0.9942616
#> Fiat 128             0.9780049 0.9985037 1.0000000     0.9349039      0.9979526
#> Maserati Bora        0.9851604 0.9489268 0.9349039     1.0000000      0.9279730
#> Toyota Corolla       0.9697187 0.9942616 0.9979526     0.9279730      1.0000000
#> Ferrari Dino         0.9836829 0.9542644 0.9431153     0.9971614      0.9408047
#> Lotus Europa         0.9838514 0.9752213 0.9709341     0.9809147      0.9735639
#>                     Ferrari Dino Lotus Europa
#> Pontiac Firebird       0.8654714    0.8566400
#> Hornet 4 Drive         0.8588699    0.8552800
#> Lincoln Continental    0.8781242    0.8656163
#> Merc 240D              0.8486224    0.8564798
#> Dodge Challenger       0.8801175    0.8709701
#> Valiant                0.8765768    0.8722962
#> AMC Javelin            0.8889901    0.8797277
#> Chrysler Imperial      0.9004177    0.8883790
#> Merc 450SLC            0.9415899    0.9307268
#> Merc 450SE             0.9416717    0.9311976
#> Merc 450SL             0.9417605    0.9316971
#> Duster 360             0.9483130    0.9350718
#> Camaro Z28             0.9526674    0.9389672
#> Mazda RX4              0.9495308    0.9467139
#> Mazda RX4 Wag          0.9494473    0.9467045
#> Merc 230               0.9425465    0.9456460
#> Ford Pantera L         0.9634519    0.9498219
#> Merc 280C              0.9600815    0.9541565
#> Merc 280               0.9601352    0.9550090
#> Porsche 914-2          0.9594920    0.9660054
#> Honda Civic            0.9089867    0.9397725
#> Toyota Corona          0.9697639    0.9725778
#> Datsun 710             0.9761739    0.9811911
#> Volvo 142E             0.9836829    0.9838514
#> Fiat X1-9              0.9542644    0.9752213
#> Fiat 128               0.9431153    0.9709341
#> Maserati Bora          0.9971614    0.9809147
#> Toyota Corolla         0.9408047    0.9735639
#> Ferrari Dino           1.0000000    0.9908847
#> Lotus Europa           0.9908847    1.0000000