2
votes

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?

1

1 Answers

1
votes

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:

library(ggplot2)
data <- iris[1:4]
labels <- iris[,5]
res <- princomp(data)
res.proj <- as.matrix(data) %*% res$loadings[,1:2]
ggplot(as.data.frame(res.proj), 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(as.data.frame(res.proj), aes(PC1, PC2, col=labels)) + geom_point()

enter image description here

Fancier ggbiplot:

library(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')
print(g)

enter image description here