0
votes

I would like to plot the results of the robust PCA (pcaCoDa) from the robCompositions package using ggplot2.

Previously, it worked with ggbiplot (https://github.com/vqv/ggbiplot) however, I can no longer get it to work with my current R version (3.6.0).

Is there a way to do a biplot with the pcaCoda results with ggplot2 using CRAN packages? Here is a working example without using ggplot:

library(robCompositions)
df <- arcticLake
a <- pcaCoDa(df)
biplot(a)

And another example without using the robust PCA, but using the autoplot function:

library(ggplot2)    
autoplot(princomp(df))

However, I would like to use the robust PCA with ggplot/autoplot. When I try to plot it, i get the following error:

autoplot(a)

Error: Objects of type pcaCoDa not supported by autoplot.

I also tried the following and also get an error:

autoplot(a$princompOutputClr)

Error in scale.default(data, center = FALSE, scale = 1/scale) :
length of 'scale' must equal the number of columns of 'x'

Any advice? Thanks!

1

1 Answers

1
votes

For some reasons that I ignore pcaCoda returns one value less for scale and center compared to the output of other pca methods such as prcomp or princomp. I think that's the reason why autoplot does not want to plot this object.

Alternatively, if you want to apply the robust algortithm, you can use the package pcaMethods available on bioconductor, here i provided an example using the iris dataset that you can found on the documentation of pcaMethods (https://bioconductor.org/packages/release/bioc/html/pcaMethods.html):

if (!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("pcaMethods")

library(pcaMethods)
library(ggplot2)

robust = pca(iris[c(1, 2, 3, 4)], method = "robustPca", scale = "uv", center = TRUE)
iris = merge(iris, scores(robust), by =0)

ggplot(iris, aes( x= PC1, y = PC2, colour = Species))+
  geom_point()+
  stat_ellipse()

enter image description here

Does it look what you are trying to get ?