0

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?

Dan
  • 11,370
  • 4
  • 43
  • 68
  • 1
    From a reading of the documentation, I would provide a vector supplying the 8 components to the `ggbiplot(df, choices = )` argument. You might be able to do this with a logical vector such as `choices = c(pc >= 1)` If you supply some data and the code you are using as a minimal example (see [here](https://stackoverflow.com/help/mcve)) answers might be more helpful. – bob1 Feb 28 '19 at 16:07
  • Thanks for your comment, bob1. But would that then only plot the 8 orders I'm interested in and nothing else? I should've clarified in my question, I want to have the information for all of the orders being graphed into the ggbiplot, I just want to remove all of the other arrows until there are 8 arrows total (instead of an arrow for each order, like it's automatically outputting). – Jericho Cook Feb 28 '19 at 16:29
  • It is probably correct that it will only plot the 8 orders. I am not familiar with the package at all, but you may be able to plot with the 8 selected, then overlay with all by using another call to `ggbiplot`. Otherwise, some post-processing to remove the arrows using inkscape or similar may be an idea. – bob1 Feb 28 '19 at 16:51
  • MrFlick, just added a very simple reproducible example. My only question has to do with removing some arrows from the ggbiplot and keeping others. – Jericho Cook Feb 28 '19 at 17:43

1 Answers1

1

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)

Dan
  • 11,370
  • 4
  • 43
  • 68