Search code examples
rggplot2ggfortifyggrepel

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")

Solution

  • You can use loadings.label.repel=T from the ggfortify package.

    This example uses your same code, just with the mtcars dataset.

    Without repelled labels:

    library(ggplot2)
    library(ggfortify)
    
    autoplot(prcomp(mtcars),
             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") 
    

    enter image description here

    With repelled labels:

    autoplot(prcomp(mtcars),
             loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, 
             loadings.label.size = 4, loading.label.color = 'red',loadings.label.repel=T) +
      ggtitle(label = "Principal Component Analysis and Varimax Rotation for Built 
              Environment Indicators") 
    

    enter image description here