3

I am creating a PCA biplot with multivariate data.

Is there a way to specify the colour/transparency/position of line segments in ggbiplot? None of the arguments to this command provide this option.

I know ggbiplot is based on ggplot - does it perhaps accept aes arguments? Or can one layer colour/transparency/position over the created plot to override defaults?

Specifically, regarding position, I would like to jitter the line segments if this is possible (although making them more transparent will probably solve the problem already).

Working example, using the iris data:

#load required packages
library(ggplot2)
library(devtools)
library(ggbiplot)

#load dataset
data(iris)

#perform principal component analysis
pca = prcomp(iris[ , 1:4], scale=T)

#define classes, generate & view PCA biplot
class = iris$Species
ggbiplot(pca, obs.scale = 1, var.scale = 1, groups = class, circle = FALSE, 
         varname.size = 1, varname.adjust = 6)

Thanks very much - any help is appreciated!

Kind regards.

Henrik
  • 65,555
  • 14
  • 143
  • 159
JanP
  • 31
  • 1
  • 2

2 Answers2

8

It seems that you need to change the ggbiplot function slightly. Type ggbiplot in the console, copy the code to an editor. In the arglist in the function, add "name = expression" terms for color, line type and transparency ("alpha") for the arrows.

ggbiplot2 <- function (pcobj, choices = 1:2, scale = 1, pc.biplot = TRUE, 
          obs.scale = 1 - scale, var.scale = scale, groups = NULL, 
          ellipse = FALSE, ellipse.prob = 0.68, labels = NULL, labels.size = 3, 
          alpha = 1, var.axes = TRUE, circle = FALSE, circle.prob = 0.69, 
          varname.size = 3, varname.adjust = 1.5, varname.abbrev = FALSE, 
          color = muted("red"), # <- add new arguments to the function
          linetype = "solid",
          alpha_arrow = 1) 

Then search for the geom_segment part, and add arguments for color, linetype and alpha:

g <- g + geom_segment(data = df.v, aes(x = 0, y = 0, xend = xvar, yend = yvar),
                      arrow = arrow(length = unit(1/2, "picas")),
                      color = color, linetype = linetype, alpha = alpha_arrow)

Assign the edited function to a new name, e.g. ggbiplot2. Try it, where you set values other than the default for the arrows:

ggbiplot2(pca, obs.scale = 1, var.scale = 1, groups = class, circle = F, varname.size = 1, varname.adjust = 6,
color = "blue", linetype = "dashed", alpha_arrow = 0.5) # <- use the new arguments

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Big thanks for this @Henrik, this was a big help. I also added a line.size argument to pass along to size because the default arrow widths are quite small. – Bajcz Apr 12 '17 at 13:45
2

Thanks for you suggestions.

I wrote the function so that it can be easily used. Also you can specify which columns (=variables) to select and which colour to assign to each of the selected variables. So far it is only based on princomp(), since I did not use the other functions for PCA yet. Feel free to contribute:

https://github.com/EhrmannS/r-snippets/blob/master/graphics/ggplot/ggbiplot_more_graphics_options.R

simply specify:

g <- ggbiplot2(pcobj, 
               coi <- list(c("variables", "with", "first", "colour"), 
                           c("variables", "with", "second", "colour")), 
               arrow.alpha = c(0.2, 1, 1),
               arrow.color = c(muted("red"), "black", "red"))

along with your other arguments.