user6848554 user6848554 - 5 months ago 49
R Question

How to plot a new vector onto a PCA space in R

I am new to principal component analysis in R and my question is quite naive. I have done a PCA of a matrix (A) using the function 'prcomp' in R. Now I want to plot a vector onto the PCA space of PC1 and PC2 of A. How do I come about this plotting of the vector?


Use biplot (red arrrows are the dimensions in the original space):

a <- princomp(iris[1:4])
biplot(a, cex=0.5)

enter image description here

you can do the projection to the PCA space on your own too as follows:

data <- iris[1:4]
labels <- iris[,5]
res <- princomp(data)
res.proj <- as.matrix(data) %*% res$loadings[,1:2]
ggplot(, aes(Comp.1, Comp.2, col=labels)) + geom_point()

Same plot using prcomp (numerically more stable):

data <- iris[1:4]
labels <- iris[,5]
res <- prcomp(data)
res.proj <- as.matrix(data) %*% res$rotation[,1:2]
ggplot(, aes(PC1, PC2, col=labels)) + geom_point()

enter image description here

Fancier ggbiplot:

g <- ggbiplot(res, obs.scale = 1, var.scale = 1, 
              groups = labels, ellipse = TRUE, 
              circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal', 
               legend.position = 'top')

enter image description here