Specifying color, opacity and position of arrows (line segments) in ggbiplot

I am creating multiple PCA with multivariate data.

Is it possible to specify the color / opacity / position of line segments in ggbiplot

? None of the arguments to this command provide this option.

I know which is ggbiplot

based on ggplot

- maybe it takes arguments aes

? Or could a single layer color / transparency / position above the generated graph override the defaults?

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

The R dataset "iris" is fine to use as an example with my script (which works):

#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
pca_biplot = ggbiplot(pca,obs.scale = 1, var.scale=1,groups=class,circle=F,varname.size=1,varname.adjust=6)
data_pca

      

Thanks a lot - any help is appreciated!

Sincerely.

+3


source to share


2 answers


You seem to need to change the function a little ggbiplot

. Enter ggbiplot

in the console, copy the code to the editor. In arglist

in, function

add the terms "name = expression" for color, linetype, and transparency ("alpha") for 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 part geom_segment

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, eg. ggbiplot2

... Try where you are setting 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

+6


source


Thanks for your suggestions.

I wrote a function so that it can be used easily. You can also specify which columns (= variables) to select and what color to assign to each of the selected variables. So far it is only based on princomp (), as I haven't used other functions for PCA yet. Feel free to contribute:

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



just indicate:

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.

+2


source







All Articles