Search code examples
rpcaprcomp

What is the line in a 3D pca and its meaning?


Recently, I focused on 3D PCA. And I know how to produce 3D PCA plots through different packages in R, such as plotly, rgl and so on.

But I have a small question from the picture below:

I don't know how to add vertical lines in R just as the picture showed.

And it is only used to locate sample points on PC1 ? Or other special meaning ?

Here is a simple example data and code: But I am not sure vertical lines are added by this package.

library(rgl)
pc <- prcomp(~ . - Species, data = iris, scale = TRUE)

scores = as.data.frame(pc$x)
plot3d(scores[,1:3],
       
       main="ABC")

I hope somebody could give me some advice or solutions.

Thanks in advance.

enter image description here


Solution

  • This is not pretty, but to reproduce something like this in rgl, you can used segments3d:

    library(rgl)
    pc <- prcomp(~ . - Species, data = iris, scale = TRUE)
    
    scores = as.data.frame(pc$x)
    plot3d(scores[,1:3],
      main="ABC")
    
    lims <- apply(scores[,1:3], 2, range)
    
    apply(scores[,1:3], 1, FUN = function(X){
      segments3d(
        x = rep(X[1], 2), 
        y = rep(X[2], 2),
        z = c(X[3], lims[1,3]))
    })
    

    enter image description here

    You may want to look into using persp and trans3d for more control.