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.
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

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With