biplot in R labeling by two variables?

Viewed 183

Hello I have a multivariate dataset

I performed a PCA over my scaled data and used fviz_pca_biplot() function for displaying a biplot.

this is a example of what I did:

(example with mtcars)

colnames(mtcars)

"mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb"

ds <- mtcars %>%
  scale()
PCA_analysis <- princomp(ds)
fviz_pca_biplot(PCA_analysis, label = "all", habillage =  mtcars$gear) 

pca_mtcars

The issue is that I want to display the labeling not only by "gear" variable but by "am" variable of mtcars dataset, in a way that the point colors are relative to "gear" variable and the shape of the points are relative of the "am" variable

1 Answers

No easy way to do it, but you can try only plotting the text:

g = fviz_pca_biplot(PCA_analysis, label = "all", 
habillage =  mtcars$gear,geom="text",show.legend=FALSE) 

Then you add the data manually to the ggplot object and complete by calling geom_point() :

g$data$am = factor(mtcars$am)
g$data$gear = factor(mtcars$gear)
g + geom_point(aes(color = gear,shape = am))

enter image description here

Related