How to put several formats in the points of my PCA by ggplot2

3

I would like to put different shapes in the graph points of a PCA generated by ggplot2 for example (masculinos = square, feminine = triangle ...).

    
asked by anonymous 17.02.2017 / 03:34

3 answers

2

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.

    
17.02.2017 / 03:43
2

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)

    
14.03.2017 / 02:35
1

It is also possible to do in the base R.

Example:

ir.pca <- prcomp(iris[,1:4]) 
plot(ir.pca$x[,1], ir.pca$x[,2], pch = as.numeric(iris$Species),
     xlab = "PC1", ylab = "PC2")
legend("topright", pch = unique(as.numeric(iris$Species)), 
       legend = unique(iris$Species))

    
25.02.2017 / 17:12