ahmohamed / lipidr

Data Mining and Analysis of Lipidomics datasets in R

Home Page:https://www.lipidr.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PCA loading plot

rosanmar opened this issue · comments

Hi, I would like to know if it is possible to obtain the loading plot in the PCA analysis. I can only get the Score plot. It would help me a lot to have a more accurate view of the data. Thanks!

You can easily access rotation scores and feature loadings in the mvaresults object that you get out of mva

mvaresults <- mva(data_normalized, measure = "Area", method = "PCA")
head(mvaresults$loadings)
#>            p1            p2          p3            p4          p5         p6
#> 1 -0.04486626 -0.0233747391 -0.02287171 -0.0001824736 -0.11510403 0.10940792
#> 2 -0.06824449 -0.0041885108 -0.10580046 -0.0271234534 -0.05546431 0.04938349
#> 3 -0.05685793  0.0005886418 -0.08676486 -0.0105830023 -0.05827554 0.09475925
#> 4 -0.07189545  0.0122874373 -0.08121652 -0.0047797565 -0.07815611 0.08942695
#> 5 -0.05769839  0.0191061415 -0.06930820  0.0074809106 -0.12747176 0.06469317
#> 6 -0.06703077 -0.0172104696 -0.08123090 -0.0222336836 -0.02194360 0.14513379
#>             p7           p8           p9         p10          p11        p12
#> 1  0.041422864 -0.123401876  0.062488158 -0.07354506 -0.072472752 0.06127880
#> 2 -0.002641356  0.024756163 -0.027674854  0.03606265 -0.008551648 0.10877558
#> 3 -0.002939514 -0.024244310  0.022280400  0.02903706 -0.028059333 0.04165076
#> 4  0.004178734  0.010924320 -0.020351285  0.04147909 -0.002295194 0.06791264
#> 5  0.042523774 -0.009729941 -0.017658042  0.07323154 -0.080672000 0.04562555
#> 6  0.051002617 -0.032604989  0.001773531  0.01593442 -0.005125500 0.01516734
#>            p13         p14          p15          p16          p17         p18
#> 1 -0.101870593  0.09815173  0.141787153  0.034732585  0.059012853  0.03850636
#> 2  0.001371712  0.02484810  0.065049476  0.028121947 -0.015897704 -0.00527741
#> 3 -0.066706070 -0.04278449 -0.092226897  0.054104472 -0.154535843  0.05144231
#> 4 -0.043459893  0.01568352 -0.001005736  0.008276927 -0.004047336 -0.02446107
#> 5 -0.029622134  0.02087661  0.047224964 -0.126033033  0.025858293 -0.11239465
#> 6  0.056604628 -0.02346504 -0.058938814  0.031138657 -0.044391858 -0.04313277
#>           p19          p20           p21          p22          p23          p24
#> 1  0.08735057 -0.023171998  0.1327614587  0.143515538  0.017939290  0.022034086
#> 2  0.11714317  0.002029921  0.0863722055 -0.013607537  0.007089923  0.013205412
#> 3 -0.07283001  0.091112524 -0.0849109083  0.143208236  0.009746848  0.012963286
#> 4  0.04687795 -0.012546376  0.0733380260 -0.031099794 -0.002185009  0.020077987
#> 5  0.05266569 -0.019254358  0.0936993506  0.051854809  0.044529168 -0.051850745
#> 6  0.01738002  0.034460499  0.0006764378  0.000419968 -0.008940267 -0.003577616
#>           p25          p26          p27           p28         p29           p30
#> 1 -0.08018621 -0.185873658  0.071979006 -0.1797587379 -0.01475394 -0.0898808441
#> 2  0.05753981  0.078977584 -0.001519507 -0.0001887463  0.03304144  0.0316588592
#> 3 -0.13430893 -0.120268941  0.065336383  0.0694995333 -0.04210952 -0.1065655993
#> 4 -0.01315190  0.029543886 -0.030397421  0.0164037116  0.02957865  0.0004006193
#> 5 -0.04443170  0.062118642 -0.111382037 -0.0002494434 -0.14720892 -0.1444244667
#> 6 -0.03688617 -0.008309826 -0.026150740 -0.0183052204  0.01578169 -0.0076053224
#>           p31          p32          p33         p34          p35         p36
#> 1  0.19680094 -0.073313211  0.143210303  0.11575151  0.079779108 -0.12212705
#> 2 -0.01716728  0.015515933 -0.006823624  0.01058728  0.067893446  0.00434836
#> 3  0.20716507  0.151799833 -0.048203747  0.02706126 -0.086087612 -0.02546880
#> 4  0.05332261 -0.002234615 -0.002740621  0.02195654  0.038125575  0.03832301
#> 5 -0.12189552 -0.111593044  0.100929223 -0.04682216  0.122059894 -0.11715653
#> 6  0.04013675 -0.013802894 -0.046485265  0.02199375 -0.008693271 -0.01381856
#>            p37           p38          p39           p40          p41
#> 1 -0.064761343  0.0464734042 -0.018826614 -0.1625078046  0.022150272
#> 2  0.020340294 -0.0086062917 -0.013022199  0.0227267812 -0.013628586
#> 3  0.089553876  0.2392170503  0.141294601  0.0507552024 -0.004970093
#> 4  0.026502505  0.0009704052 -0.005765791  0.0079784152 -0.002567018
#> 5  0.071363136 -0.0472534963 -0.196216872 -0.0006502088  0.093888513
#> 6  0.002937943 -0.0624690809 -0.014324027 -0.0249568821 -0.010661841
#>            p42         p43          p44         p45         p46         p47
#> 1  0.061575593  0.06371584  0.002368088  0.05398453 -0.04479277  0.07029665
#> 2 -0.040983679  0.04660136  0.031113107 -0.04166941  0.05449903  0.06638949
#> 3 -0.007192564 -0.13055651 -0.050809408  0.05753822 -0.10570869  0.09074203
#> 4  0.023666501 -0.10090235  0.145264101  0.01842521  0.04465017 -0.07046376
#> 5 -0.041274547  0.05219034 -0.113364519 -0.09273369 -0.01139080  0.06016911
#> 6 -0.097450820  0.12884173  0.042805271  0.03738526 -0.01062511 -0.01198347
#>            p48          p49          p50          p51         p52         p53
#> 1 -0.039157610 -0.018659476  0.025656832  0.015796183 -0.03123130  0.10398039
#> 2  0.076567358  0.100391995  0.030225296 -0.031444802  0.07215529  0.03700429
#> 3  0.140156318  0.146798496 -0.000348664  0.208762054  0.02683236  0.02217379
#> 4  0.009997167  0.031175316 -0.058012141 -0.081188864  0.03573016  0.06084736
#> 5  0.016585190 -0.007281378 -0.033409371 -0.003697608  0.07778175  0.08738922
#> 6 -0.078560315 -0.048563258 -0.058780513 -0.008313705  0.10416144 -0.03026025
#>            p54          p55           p56
#> 1 -0.022447268  0.029989331  1.677175e-08
#> 2 -0.102920138 -0.003963734  3.789119e-08
#> 3 -0.081605517 -0.052167020  1.099639e-08
#> 4  0.012820158 -0.013551350  3.090635e-08
#> 5  0.039494155  0.092428200  3.100631e-08
#> 6 -0.004072756  0.037759084 -2.787232e-08
head(mvaresults$scores)
#>             p1         p2       p3         p4         p5        p6         p7
#> S1A  -3.389052  -9.783666 2.170264 -0.1381608  0.3898506 -7.547779  1.7744696
#> S2A  -8.617481  -9.519327 2.155308 -2.1006989  0.1635756 -5.458006  0.2970577
#> S3A  -8.093241  -6.082544 3.648055 -1.3647316  1.9769127  2.200922 -4.3734197
#> S4A -11.230227 -11.793822 2.828469  1.2301022 -1.9791670  2.846245  4.1847916
#> S5A -11.046872 -10.568540 1.614010 -2.9355782  0.9754839 -2.248875  2.6948012
#> S6A -11.217205  -7.553303 4.539788  1.4530670 -2.2743519  5.027765  4.6714424
#>             p8        p9        p10        p11        p12        p13
#> S1A -0.5527409  1.609809 -1.0974229  1.7643018 -1.0622011  3.9603446
#> S2A -1.3562399  1.020424  1.1821638 -0.2822290 -0.8105609  0.5022525
#> S3A  0.4786930  7.505775 -3.0457816  1.8156550  2.9885354  0.5422828
#> S4A  2.6243623 -5.054472 -1.9918639 -0.4520386  1.4328927  1.3878084
#> S5A -0.3135096  1.551402 -0.3997732  1.1751316 -1.7143864 -4.3496838
#> S6A  5.6468546 -2.640643 -0.9740625 -2.5520324 -1.3991995  0.2086560
#>             p14         p15        p16         p17        p18        p19
#> S1A -0.98753330  0.18144563 -2.3919224  0.03335976 -0.0549729  2.9940190
#> S2A -2.57110796 -1.31631840 -0.5033193  0.57023702 -1.6670968  0.6620278
#> S3A  0.08387708 -1.03239241 -1.3562597 -0.41041365  4.7716781 -1.4251001
#> S4A -0.34734940  0.06740679 -2.2907055 -1.93567801  0.8966249 -1.3748868
#> S5A -0.38734848 -1.12376955  1.1327551  0.54339140 -0.9577151 -1.5715385
#> S6A -1.87416718 -1.52561389 -0.5245885 -0.92741859  0.4059875 -1.1882350
#>            p20        p21        p22        p23         p24         p25
#> S1A -0.9453414  1.8981966 -1.6873041  0.5985171 -1.15171465 -0.13250058
#> S2A -0.1947738 -1.2777145  0.1143270 -0.2617331 -0.82949445  0.06194476
#> S3A -1.3198005 -0.7707923 -1.0298894  0.2215337  0.53882718 -0.58213914
#> S4A -0.9966085 -1.4198805  0.7901539  0.2433820  1.42759416 -0.31621381
#> S5A  1.0288627 -0.6159911  0.7495257  0.2617134  0.48701917  1.13458096
#> S6A  0.1532440  2.1799007  0.1015316 -0.5883413  0.02968717 -0.03153017
#>             p26        p27         p28        p29         p30        p31
#> S1A -0.83531219  1.1782938  0.46563737  0.9595102  0.42808437 -0.4474506
#> S2A  1.45780695 -0.3339886 -0.67661162 -1.2998638  0.05393809 -0.2947462
#> S3A  1.17238389 -0.3722343  0.79027247  0.9882649  0.15908415  0.3004675
#> S4A -0.02359825  0.1263715 -1.59178275 -0.8888971  1.19893748 -0.9644964
#> S5A  0.55233896  0.3443943 -0.01768317 -0.7205369  0.60387511 -0.5576576
#> S6A  0.13671744  0.6636323  1.23199624  0.7048083 -1.93376292 -0.7303524
#>            p32        p33        p34        p35         p36         p37
#> S1A -0.5154476 -0.8254394 -0.1644401  1.0272981  0.48768823  0.06574612
#> S2A  0.3443014  1.1049848  0.8609025 -0.6980224 -0.75710563  0.05084846
#> S3A  0.7301498  0.7323349 -0.4600249 -0.6966259  0.06456891  0.14305978
#> S4A -0.2872269 -0.2851235  0.7056463  0.1619820 -0.21096218 -0.68311746
#> S5A  0.5323703 -1.6969022 -0.1499577 -1.4140969  0.14364860 -0.40110845
#> S6A  0.6329406  0.8282167 -0.9095093  0.9184934 -0.36437165  0.35065205
#>            p38         p39         p40         p41         p42         p43
#> S1A  0.2705323  0.02425644  0.17021476  0.98507785  0.17433633 -0.13801865
#> S2A  1.0515692 -0.66412382 -0.64986922  0.01307083  0.29890552  0.14720659
#> S3A -0.5121970 -0.09959737 -0.02354705  0.43273435  0.07568789  0.01727799
#> S4A -0.5119569  0.18028081  0.14333264  0.34905603  0.55680782 -0.09145678
#> S5A -0.1503646 -0.36463465 -0.31910584  0.85898450 -1.12895306  0.11878245
#> S6A  0.7249559  0.06383262  0.03433319 -0.35261103 -0.64743849  0.35302887
#>            p44         p45        p46         p47         p48          p49
#> S1A -0.4285355 -0.14164237 -0.5056658 -0.36088144 -0.13365404 -0.072973728
#> S2A  1.2974050  0.63204517  0.2729308  0.18178705 -1.47856026  0.307450676
#> S3A  0.1129909 -0.06578999 -0.1101750  0.08656873 -0.23771047 -0.178336149
#> S4A -0.8569712 -0.21693871  0.4989003 -0.95013898  0.18870139  0.391694852
#> S5A -0.6176003 -0.53750168  0.2430985  0.69075635  0.24233823 -0.367584487
#> S6A  0.2210085  0.44719414 -0.3317279  0.95764767 -0.03981357  0.002108038
#>            p50          p51         p52        p53          p54         p55
#> S1A -0.5130158 -0.010960726  0.04925003  0.7524236  0.109834104  0.12430692
#> S2A -0.2225566 -0.220043048  0.19979093 -0.4599694 -0.284382492  0.06240105
#> S3A  0.3530716  0.003229078 -0.02687068 -0.2243258  0.109630101 -0.10789731
#> S4A -0.2258803 -0.327339574  0.09504706 -0.5460712 -0.115620791  0.09043329
#> S5A  0.0389916  0.261059074 -0.34763108  0.8268310  0.340989129  0.49375362
#> S6A  0.2948872 -0.033388944  0.10736295  0.1780890 -0.006969587 -0.18751794
#>               p56
#> S1A -1.406289e-07
#> S2A -1.406289e-07
#> S3A -1.406289e-07
#> S4A -1.406289e-07
#> S5A -1.406289e-07
#> S6A -1.406289e-07

Created on 2022-10-10 by the reprex package (v2.0.1)

Thank you !!!, but is it possible to obtain the PCA loading plots (the visualization), as is done with OPLS or OPLS-DA (plot_mva_loadings(mvaresults, color_by="Class", top.n=10))?

Sorry I didn't realise you're after plotting them. For now, you can use lipidr internal functions to help you do that. I'll consider adding support in the near future.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.1
mvaresults <- mva(data_normalized, measure = "Area", method = "PCA")
m = lipidr:::.get_loading_matrix(mvaresults, components = c(1,2), color_by = "Class")
ggplot(m[[1]], aes(p1, p2, color=Class)) + geom_point()

Created on 2022-10-11 by the reprex package (v2.0.1)

It works! :) thanks!