I am trying to assign different colors for variables in a PCA biplot. However, fviz_pca_biplot
from the R package factoextra
can not plot the correct color for each variable.
library(factoextra)
data(iris)
res.pca <- prcomp(iris[, -5], retx = TRUE, center = TRUE, scale. = TRUE)
res.pca
my.col.var <- c("red", "blue", "red", "yellow")
fviz_pca_biplot(res.pca, repel = TRUE, axes = c(1, 2),
col.var = my.col.var, col.ind = "#696969",
label = "var", title = "")
I have assigned "red", "blue", "red", "yellow" for variables "Sepal.Length", "Sepal.Width", "Petal.Length", and "Petal.Width". However, the figure shows wrong colors for all variables.
In the function we must indicate the name of the variables for col.var=
and not the colors. we can then give our color manually to palette=
option. So the code would be:
library(factoextra)
data(iris)
res.pca <- prcomp(iris[, -5], retx = TRUE, center = TRUE, scale. = TRUE)
res.pca
my.col.var <- c("red", "blue", "red", "yellow")
fviz_pca_biplot(res.pca
, repel = TRUE
, axes = c(1, 2)
, col.var = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width" )
, col.ind = "#696969"
, label = c("var")
, title = ""
, palette = my.col.var
)