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