I am trying to make a PCA plot with individuals -where one categorical variable (A) would be represented as the point shape (eg one group as a circle, a second one as a square, etc.) -and a second categorical variable (B) as the color inside the point Is that possible? Which code would you use?
Asked
Active
Viewed 2,689 times
5
-
Please, could you provide your code. – Earl Mascetti Apr 11 '20 at 11:42
1 Answers
6
I don't think you can modify the output from fviz_pca_ind()
, so you would need to take out the data from the results, and plot it again using ggplot2:
library(factoextra)
library(ggplot2)
data <- iris
colnames(data)[5] <- "A"
data$B <- sample(letters[1:2],nrow(data),replace=TRUE)
res.pca <- prcomp(data[,1:4], scale = TRUE)
basic_plot <- fviz_pca_ind(res.pca, label="none")
ggplot(cbind(basic_plot$data,data[,c("A","B")]),
aes(x=x,y=y,col=A,shape=B)) + geom_point() + theme_bw()

StupidWolf
- 45,075
- 17
- 40
- 72
-
thanks a lot for this clear answer ! This is exactly what I was looking for :-) – Xav64 Apr 11 '20 at 17:23