Why PCA code is not running?

Hello, i am trying to run PCA but somehow it is not running properly. If someone can please help me. Code and data attached:
Code

library(FactoMineR)
library(Factoshiny)
#> Warning: package 'Factoshiny' was built under R version 4.0.2
#> Loading required package: shiny
#> Warning: package 'shiny' was built under R version 4.0.2
#> Loading required package: FactoInvestigate
#> Warning: package 'FactoInvestigate' was built under R version 4.0.2
#> Loading required package: ggplot2
library(factoextra)
#> Warning: package 'factoextra' was built under R version 4.0.2
#> Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(ggpubr)
#> Warning: package 'ggpubr' was built under R version 4.0.2
df <- read.csv("F:/Figures/mfa_file.csv", stringsAsFactors = F)
colnames(df)[1] = "sample_name"
df1 <- df[,-1]
fviz_eig(res.pca)
#> Error in get_eig(X): object 'res.pca' not found
fviz_pca_ind(res.pca, 
             col.ind = "cos2", pointsize = "cos2",
             gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
             repel = TRUE)
#> Error in .get_facto_class(X): object 'res.pca' not found

Created on 2020-07-07 by the reprex package (v0.3.0)

Data:
structure(list(ï..SampleName = c("Russian Banana", "Vermillion",
"Masquerade", "Rio Colorado", "POR12PG28-3", "Valery", "CO99076-6R",
"CO05068-1RU", "Canela Russet", "AC99330-1P/Y", "Atlantic", "Purple Majesty"
), Yellow_A = c(6, 0, 6, 1.5, 12, 13, 2, 1.5, 2, 11, 2.5, 0),
Purple_A = c(0, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7), Dullness_A = c(9,
9, 12, 11.5, 9.5, 9, 12, 11, 12, 11, 11, 11), Smoothness_A = c(4,
8, 11, 1.5, 9, 12, 12.5, 12, 12, 9, 12, 11), Gritty_A = c(2,
0, 0, 1.5, 2, 0, 0, 0, 0, 2, 0, 1.5), SweetAromatics_AR = c(1.5,
0, 0, 0, 0, 2, 0, 0, 0, 1.5, 0, 0), MustyEarthy_AR = c(3.5,
4, 4, 0, 0, 0, 0, 0, 2.5, 0, 0, 3.5), Beany_AR = c(0, 0,
0, 2, 2.5, 0, 0, 0, 0, 0, 2, 0), Potato_AR = c(9, 9, 7.5,
7.5, 8, 5, 8.5, 8, 7.5, 9, 7, 8.5), Earthy_AR = c(4, 0, 0,
0, 2, 0, 2.5, 2, 0, 0, 2, 0), Cooked_AR = c(6, 6, 3, 4, 4,
2, 4, 5, 4, 6, 3.5, 4), Cardboard_AR = c(3.5, 4, 2.5, 2.5,
3, 2, 2.5, 2.5, 2.5, 1.5, 3, 3.5), Umami_AR = c(4, 3.5, 3,
2, 3, 0, 3, 3, 3, 3.5, 2.5, 2.5), Metallic_AR = c(3L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L, 0L), Buttery_AR = c(2L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Vegetable.complex_AR = c(0,
0, 0, 0, 0, 0, 0, 2.5, 2, 0, 0, 0), Raw.potato.peel_AR = c(0L,
0L, 3L, 0L, 0L, 0L, 0L, 2L, 2L, 0L, 0L, 3L), Raw.Potato_AR = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Eggy_AR = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), SwtPtato_AR = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Cauliflower_AR = c(2L,
0L, 0L, 0L, 3L, 0L, 0L, 0L, 0L, 0L, 2L, 0L), OverallPotatoID_F = c(9,
8, 6.5, 8, 7, 3.5, 8, 8, 8, 9, 7, 8.5)), row.names = c(NA,
12L), class = "data.frame")

You have omitted the part where you perform a PCA on your df and stored the result in a variable named res.pca

I set the rownames of the internal pca object, and this will dicate what gets plotted
rownames(res.pca$x) <- df$sample_name


 library(factoextra)

library(tidyverse)
(df <- mtcars %>% as_tibble(rownames = "sample_name"))
res.pca <- prcomp(df[-1], scale = F)
fviz_eig(res.pca)
rownames(res.pca$x) <- df$sample_name
fviz_pca_ind(res.pca,
             col.ind = "cos2", pointsize = "cos2",
             # geom="point",
             gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
             repel = TRUE)

Thanks @nirgrahamuk for the help.
I got names back. Do you know if we can somehow remove title of the PCA plot (Individuals PCA)Rplot ?

yes, pass title = ""

fviz_pca_ind(res.pca,
             col.ind = "cos2", pointsize = "cos2",
             # geom="point",
             gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
             repel = TRUE,title="")
1 Like

Thanks @nirgrahamuk for the help. One last thing, can i move legend box also or not? If yes, how?

fviz_pca_ind(res.pca,
             col.ind = "cos2", pointsize = "cos2",
             # geom="point",
             gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
             repel = TRUE,title="") -> fpi

fpi + theme(legend.position = "left")
1 Like

Thanks, I tried the same code for specific positioning but it was not working. Do you know why?
fviz_pca_ind(res.pca,
col.ind = "cos2",
gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
repel = TRUE,
title = "") -> fpi
fpi + theme(legend.position = c(-1, 1))

yeah, I dont think position -1,1 has any meaning.
only numbers between 0 and 1 are possible for these two

1 Like

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

in your new code you skipped the previous code that you had used to remove the non numeric column

OOps! Thanks @nirgrahamuk. I got it.
But i am facing another problem here, can you please check?
Here is new code:

library(FactoMineR)
library(Factoshiny)
#> Warning: package 'Factoshiny' was built under R version 4.0.2
#> Loading required package: shiny
#> Warning: package 'shiny' was built under R version 4.0.2
#> Loading required package: FactoInvestigate
#> Warning: package 'FactoInvestigate' was built under R version 4.0.2
#> Loading required package: ggplot2
library(factoextra)
#> Warning: package 'factoextra' was built under R version 4.0.2
#> Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(ggpubr)
#> Warning: package 'ggpubr' was built under R version 4.0.2
df <- read.csv("F:/Chapter6_Files_Figures/mfa_file.csv", stringsAsFactors = F)
colnames(df)[1] = "sample_name"
res.pca <- prcomp(df, scale = F)
#> Error in colMeans(x, na.rm = TRUE): 'x' must be numeric

Created on 2020-07-07 by the reprex package (v0.3.0)

Thanks @nirgrahamuk, but why i cant have name of samples. This plot is showing numbers.Rplot
Here is code:

library(FactoMineR)
library(Factoshiny)
library(factoextra)
library(ggpubr)
df <- read.csv("F:/Chapter6_Files_Figures/mfa_file.csv", stringsAsFactors = F)
colnames(df)[1] = "sample_name"
df1 <- df[,-1]
res.pca <- prcomp(df1, scale = F)
fviz_eig(res.pca)
fviz_pca_ind(res.pca,
col.ind = "cos2", pointsize = "cos2",
gradient.cols = c("#FFCCFF", "#CC0066", "#000000"),
repel = TRUE)

There's probably a good way to map the names back onto your plot, but thats different than attempting to compute a PCA on character columns.

How? can you please show that?
Thanks