How to add PCA Loadings to ggplot?

146 views Asked by At

I have created the PCALoadings using PCAloadings <- data.frame(Variables = rownames(pca_res_t$rotation), pca_res_t$rotation) but can't seem to actually add the loadings onto my ggplot. I'll post the exact code I used and then the reproducible code as well. I've been trying to use geom_segment.

this is what I'm using to make the ggplot :

ggplot(data=sep_total_raw_data, aes(x=pc1t, y=pc2t))+
  geom_point(alpha=0.8, size=1,aes(colour=Data_Type, shape=Data_Type))+
  xlab("PC1 (53.83%)")+
  ylab("PC2 (26.3%)")+
  guides(colour=guide_legend(title="Data_Type"))+
  geom_mark_hull(concavity=5, expand=0, radius=0, aes(fill=Data_Type))+
  theme(panel.grid = element_blank(), panel.border = element_rect(fill = "transparent"))+
  geom_text(aes(label=Code, fontface="bold", colour=Data_Type))

and this is the geom_segment stuff I've tried:

 + geom_segment(data = PCALoadings, aes(x=0,xend=xvar, y=0, yend=yvar), arrow = arrow(length = unit(0.025,"npc"),type = "open"),lwd=1)

but it doesn't recognise xvar or yvar and I've tried :

+ geom_segment(data = PCALoadings, aes(x=0,xend=(pc1t*2), y=0, yend=(pc2t*2)), arrow = arrow(length = unit(1/2,"picas")), colour="black")

but it says

Error in `geom_segment()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 4th layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (5)
✖ Fix the following mappings: `xend` and `yend`
Run `rlang::last_trace()` to see where the error occurred.

here is the reproducible code:

ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width))+
  geom_point(alpha=0.8, size=1,aes(colour=Species, shape=Species))+
  xlab("Petal Length")+
  ylab("Petal Width")+
  guides(colour=guide_legend(title="Species"))+
  geom_mark_hull(concavity=5, expand=0, radius=0, aes(fill=Species))+
  theme(panel.grid = element_blank(), panel.border = element_rect(fill = "transparent"))+geom_text(aes(label=Species, fontface="bold", colour=Species))

thanks for all the help!

1

There are 1 answers

0
Beeflight31 On

So if I understand your question, you'd like to build a biplot including both individuals and variables (i.e Loadings) of the PCA.

You should try the package GABB, facilitating this kind of representation.

See the example below with the argument biplot = TRUE

library(GABB)
library(FactoMiner)


## Example of GABB package pipeline with the base data.set "mtcars" 
my.data <- mtcars

## Data preparation for RDA and PCA : tranformation and scaling of numeric/quantitative variables
prep_data(data = my.data, quantitative_columns = c(1:7), transform_data_method = "log", scale_data = T)

## Create PCA
library(FactoMineR)
my.pca <- FactoMineR::PCA(X = data_quant) 


## Create, display and save graphic output of individual and variable PCA

#Basic output with minimum required parameters
PCA_RDA_graphics(complete.data.set = initial_data_with_quant_transformed, PCA.object = my.pca, factor.names = c("vs", "am", "gear", "carb"))

#Advanced outputs (image below)
PCA_RDA_graphics(complete.data.set = initial_data_with_quant_transformed, PCA.object = my.pca, 
                 factor.names = c("vs", "am", "gear", "carb"), Biplot.PCA = TRUE,col.arrow.var.PCA = "grey",
                 Barycenter = TRUE, Segments = TRUE, Ellipse.IC = TRUE,
                 Barycenter.Ellipse.Fac1 = "vs", Barycenter.Ellipse.Fac2 = "am",
                 factor.colors = "vs", factor.shapes = "am",
                 Barycenter.factor.col = "vs", Barycenter.factor.shape = "am")

enter image description here