Principal Component analysis (PCA) in R

Поділитися
Вставка
  • Опубліковано 7 вер 2024
  • library("ggplot2")
    library("gridExtra")
    library(ggbiplot)
    library("corrplot")
    library(factoextra)
    #conversion of row number
    data=rownames(GGE)=c("Me","E6","E2","E4","KB","HUl","kat","Dan","EA2","Ts",
    "E0","E5","DI","E8","E39","ET1","BO","Ab","Si")
    pca =prcomp(GGE[,c(2:11)], center = TRUE,scale. = TRUE)
    print(pca)
    to get eigen value
    eig.val = get_eigenvalue(pca)
    eig.val
    #to get scree plot
    fviz_eig(pca, addlabels = TRUE, ylim = c(0, 50))
    PCA results for variables
    var=get_pca_var(pca)
    to see the most contributing variables for each dimension
    corrplot(var$cos2, is.corr=FALSE)
    #to see the most contributing variables for both dimension
    fviz_cos2(pca, choice = "var", axes = 1:2)
    to draw a bar plot of variable contributions
    Contributions of variables to PC1
    a=fviz_contrib(pca, choice = "var", axes = 1)# top= 5 to limit to five var.
    Contributions of variables to PC2
    b=fviz_contrib(pca, choice = "var", axes = 2)
    grid.arrange(a,b, ncol=2, top='Contribution of the variables to the first two PCs')
    Total contribution on PC1 and PC2
    fviz_contrib(pca, choice = "ind", axes = 1:2)
    #Graph of variables
    fviz_pca_var(pca,
    col.var = "cos2",
    gradient.cols = c("red", "blue", "green"),
    repel = TRUE)
    #Biplot of individuals and variables
    fviz_pca_biplot(pca, repel = TRUE,
    col.var = "blue",
    col.ind = "red")
    #########################################################################
    #Plotting PCA
    ggbiplot(pca)
    #This will name each point with the name of the genotypes
    ggbiplot(pca,labels=rownames(GGE))
    plot using PC1 and PC2
    ggbiplot(pca,ellipse=TRUE,choices=c(1,2),labels=rownames(GGE), groups=GGE$ENV)
    scale the samples
    ggbiplot(pca,ellipse=TRUE,obs.scale = 2, var.scale = 4.5,
    labels=rownames(GGE), groups=GGE$ENV)
    #remove the arrows altogether
    ggbiplot(pca,ellipse=TRUE,obs.scale = 1, var.scale = 1,var.axes=FALSE,
    labels=rownames(GGE), groups=GGE$ENV)
    final biplot by Customize ggbiplot
    aa=ggbiplot(pca,ellipse=TRUE,obs.scale = 1, var.scale = 1, labels=rownames(GGE), groups=GGE$ENV) +
    scale_colour_manual(name="Location", values= c("blue", "red", "green","pink"))+
    ggtitle("PCA of wakjira")+
    theme_minimal()+
    theme(legend.position = "bottom")
    aa
    ggsave(filename = "Waq.png", plot = aa,width = 22, height = 15, dpi = 2500, units = "cm")

КОМЕНТАРІ • 119