sinhrks / ggfortify

Define fortify and autoplot functions to allow ggplot2 to handle some popular R packages.
Other
525 stars 65 forks source link

How to derive PC values/data used by the autoplot function used in the plot #194

Closed manjusst closed 5 years ago

manjusst commented 5 years ago

Dear Developer, Hi. I used the "autoplot" function in the "ggfortify" package to make a very nice plot.

pca_genotype

Since I would like to use the PC values and eigenvalues for my publication. Could the authors explain how to get the data used for the plotting? I looked at the issue history and could not find a solution. I kindly request you to please help me out. Thanks in advance. Manju

terrytangyuan commented 5 years ago

You can call fortify() to get the raw data used for plotting. You can also find the implementation here: https://github.com/sinhrks/ggfortify/blob/master/R/fortify_stats.R#L252

manjusst commented 5 years ago

Dear Terry, I tried my best to derive the PCs used for plotting. I even tried the stats::prcomp() to look for the data. I terribly failed, since it is taking a lot of time, can you please help me with the code for this. I was happy with the plot, but since I have to mention the PC values used for plotting in the planned publication, this becomes very important Thanks in advance. Expecting a reply from you on this. Manju

terrytangyuan commented 5 years ago

@manjusst

library(ggfortify)
df <- iris[c(1, 2, 3, 4)]
fortify(prcomp(df))

You'll see the PC values like the following:

    Sepal.Length Sepal.Width Petal.Length Petal.Width          PC1          PC2          PC3
1            5.1         3.5          1.4         0.2 -2.684125626 -0.319397247  0.027914828
2            4.9         3.0          1.4         0.2 -2.714141687  0.177001225  0.210464272
3            4.7         3.2          1.3         0.2 -2.888990569  0.144949426 -0.017900256
4            4.6         3.1          1.5         0.2 -2.745342856  0.318298979 -0.031559374
5            5.0         3.6          1.4         0.2 -2.728716537 -0.326754513 -0.090079241
terrytangyuan commented 5 years ago

@manjusst Hope this helps. The best place to ask questions is in https://stackoverflow.com/questions/tagged/ggfortify instead of Github where we track technical issues. I am closing this issue for now. Please ask on StackOverflow if you have more questions. Thanks.