I would like to put different shapes in the graph points of a PCA generated by ggplot2 for example (masculinos = square, feminine = triangle ...).
I would like to put different shapes in the graph points of a PCA generated by ggplot2 for example (masculinos = square, feminine = triangle ...).
If you have set the PCA as follows:
ir.pca <- prcomp(iris[,1:4])
You can get the values of each of the components by doing:
ir.pca$x
So, to plot with ggplot, I would do so:
library(dplyr)
library(ggplot2)
data_frame(
PC1 = ir.pca$x[,1],
PC2 = ir.pca$x[,2],
Species = iris$Species
) %>%
ggplot(aes(x = PC1, y = PC2, shape = Species)) + geom_point()
Note that I added an argument shape = Species
to indicate that the shape of the plot points should be different depending on the species.
Of course your database should be different, but at first you just have to adapt this code.
The FactoMiner package in combination with the factoextra package was designed to address this situation. Note that quali.sup, in the PCA function, and habillage, in the fviz_pca_ind function, are the categorical variable.
Also, since the base is ggplot, you can customize it as you like.
ir.pca<-PCA(iris,quali.sup = 5,graph=F)
fviz_pca_ind(ir.pca,geom="point",habillage = 5)