rpcaggbiplot

How can I remove arrows from ggbiplot of PCA in R that are not significant?


So, I am attempting to create a ggbiplot of a PCA of prey order in the diet of diurnal and nocturnal raptors, but the problem is that the ggbiplot function automatically creates arrows for each order. There are only about 8 orders that are significant for my research (i.e., have a value in PC1 and PC2 that is greater than or equal to 0.1).

This is what the ggbiplot looks like right now:

enter image description here
I've also been able to successfully remove all of the arrows using the var.axes = FALSE function to get this plot:

enter image description here
But the problem is that from either plot, I'm not sure how to either remove only a portion of the arrows so that I can just keep the 8 that I need, or add those 8 back to the plot from scratch after I remove all of the arrows.

Edit: I want the PC values for all 38 orders to still be factored into the plot, I just want to remove the unnecessary arrows until there are just 8 left.

Reproducible Example:

#load iris data
iris$Species = NULL # (to run the PCA)
iris_pca = prcomp(iris)
ggbiplot(iris_pca) + theme_classic()

So, let's say I don't want to include the arrows for Sepal.Width or Petal.Width. How would I remove those and keep the other two?


Solution

  • Looking at the code here, it doesn't look like you can. There is a cludge, shown below, where you alter the resulting ggplot2 object.

    # Load library
    library(ggbiplot)
    #> Loading required package: ggplot2
    #> Loading required package: plyr
    #> Loading required package: scales
    #> Loading required package: grid
    
    # Remove species
    iris$Species <- NULL
    
    # Perform PCA
    iris_pca <-  prcomp(iris) 
    
    # Create ggbiplot
    g <- ggbiplot(iris_pca) + theme_classic()
    
    # Before plot
    plot(g)
    

    # Get ggplot2 object
    g <- ggplot_build(g)
    
    # Remove unwanted arrows & labels, say, Petal.Length Petal.Width
    g$data[[1]] <- g$data[[1]][-(3:4), ]
    g$data[[3]] <- g$data[[3]][-(3:4), ]
    
    # Repackage & plot
    plot(ggplot_gtable(g))
    

    Created on 2019-03-01 by the reprex package (v0.2.1)