Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ggfortify and ggrepel for pca

I am running a principal component analysis with a varimax rotation and wish to display the plot which seems simple enough, however my loading vectors are very close in some places and the labels of which factor they are tend to overlap. That is where ggrepel comes in in order to separate the lables. My dilemma now is figuring out how to connect the two. I used auto plot which automatically adds the desired text and it is making it difficult to define which text to repel. There may be other ways of going about it and I am open to suggestion. I have my code that works but has overlap and one of my attempts to repel the code below.

autoplot(prcomp(built.df9),
loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, 
loadings.label.size = 4, loading.label.color = 'red') +
ggtitle(label = "Principal Component Analysis and Varimax Rotation for Built 
Environment Indicators") +
geom_text_repel(aes(label = rownames(prcomp(built.df9))))

enter image description here

autoplot(prcomp(built.df9),
loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, 
loadings.label.size = 4, loading.label.color = 'red') +
ggtitle(label = "Principal Component Analysis and Varimax Rotation for Built 
Environment Indicators")
like image 317
E. Nicholson Avatar asked Jun 23 '17 19:06

E. Nicholson


1 Answers

You haven't provided any data to make this reproducible, however you may have more luck with the package, ggbiplot.

library(ggbiplot)

data(mtcars)

standardised<-as.data.frame(scale(mtcars[2:ncol(mtcars)]))

mtcars.pca<-prcomp(standardised,retx=TRUE)

ggbiplot(mtcars.pca, obs.scale=1, var.scale=1,  ellipse=F, circle=F,labels.size = 4)

enter image description here

like image 74
J.Con Avatar answered Nov 11 '22 17:11

J.Con