How can I change labels' names in PCA score plot?

My file's name is "aroma."
I want to have a score plot, so I type the below text..

res.pca <- PCA(aroma[,-1], graph = FALSE)
fviz_pca_ind(res.pca, col.ind = "cos2",
gradient.cols = c("#00AFBB", "#E7B800", "#FC4E07"),
repel = TRUE # Avoid text overlapping (slow if many points)
)
The result comes out like this. The sample names are named by 1,2,3,4.....which I don't expect.

The question is how can I change these default label names to my original sample name like MC1-1, MC1-2, etc (In Column A of my excel sheet)

Thanks in advance!!

Hi @Blueberry,
Welcome to the RStudio Community Forum.

This type of PCA plot uses the row names of the dataframe to label the plots. I cannot find any other way to specify them. In your case the row names are simply numbers from 1:nrow(aroma). So, try altering your dataframe like this before analysis and plotting:

row.names(aroma) <- aroma[, 1]  # Copy first column of dataframe to row names 

p.s. In future it is best to tell us which packages you are using. There are 12 packages which have a function PCA()!

HTH

Thanks a lot! :slight_smile:

May I ask how can I deal with the missing values in PCA?
I try to follow the info in this article by using missMDA to deal with the missing values. But I'm stuck by the error messages. http://juliejosse.com/wpcontent/uploads/2018/05/DataAnalysisMissingR.html

library(psych)
library("FactoMineR")
library("factoextra")
library(missMDA)
aroma<-read.csv(file="GCMS_overview.csv",header = TRUE)
nb <- estim_ncpPCA(aroma,method.cv = "Kfold", verbose = FALSE)
Error in estim_ncpPCA(aroma, method.cv = "Kfold", verbose = FALSE) :
The following variables are not quantitative: Aroma.Compounds
The following variables are not quantitative: Fl1
The following variables are not quantitative: Fl2
The following variables are not quantitative: Fl3
The following variables are not quantitative: Fl4
The following variables are not quantitative: Fl5
The following variables are not quantitative: Fl6
The following variables are not quantitative: Fl7
The following variables are not quantitative: Fl8
The following variables are not quantitative: Fl9
The following variables are not quantitative: Fl10
The following variables are not quantitative: Fl11
The following variables are not quantitative: Fl12
The following variables are not quantitative: Fl13
The following variables are not quantitative: Fl14
The following variables are not quantitative: Fl15
The following variables are not quantitative: Fr1
The following variables are not quantitative: Fr2
The following variables are not quantitative: Fr3

Column A is sample names, Row 1 is aroma names

Hi @Blueberry,
The error messages are telling you that all your columns are "not numeric" which means that they have probably been read in as "text" ("character"). Its hard to fix this without having a copy of your data file or a pasted copy of the dataframe aroma (screen shots are not suitable).

str(aroma) will show you the type of the various columns.

A few things to consider:
How have missing values been coded in your .csv file?
Are zero values actually "missing values"? If so, they will need to be converted to NA.
Do some columns have all missing values or all zeros? If so, those columns will contribute nothing to a PCA analysis; remove them from the dataframe before proceeding.
Text columns will need converting to numeric.

If these ideas are pretty new to you, its probably time to read a basic R primer (there are many free resources online).

HTH

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.