Specify different pointshapes for var and ind in fviz_pca_biplot

2.2k views Asked by At

Is there any way of specifying shape for variables in fviz_pca_biplot() from R package FactoExtra?

For example I have the following code:

data("iris")

PCA.iris <- prcomp(iris[ , -5], scale=TRUE)

BiPlot<- fviz_pca_biplot(PCA.iris, 
                      geom = c("point", "text"), 
                      geom.var = c("point", "text"),
                      palette="ucscgb",
                      label="var",       
                      col.ind=iris$Species,
                      invisible="quali",
                      pointshape = 19, pointsize = 3) +
                      labs(colour= "Species" )+
                      theme_gray() +
                     theme(plot.title = element_text(hjust = 0.5,size = 20, face = "bold"),
                           legend.title = element_text(size = 12), 
                           legend.text = element_text(size = 8),
                           legend.key.size = unit(0.5,"line") )+
                     guides(shape = guide_legend(override.aes = list(size = 4)),
                           color = guide_legend(override.aes = list(size = 4)) )
print(BiPlot)

but it makes the shape for both var and ind the same, I would like the shape for var to be different (shape 15). The argument "pointshape" seems to apply to both var and ind when geom.var = c("point", "text") is set to point.

I have tried using scale_shape_manual() :

 BiPlot+
 scale_shape_manual(values=15) 

but it does not apply, I am guessing that this is due to "point" in geom uses geom_point and I can not specify to which point I want to apply it but this is just a wild guess.

Is it possible to set pointshape for var and ind to different values and how is it done then?

1

There are 1 answers

1
StupidWolf On BEST ANSWER

You use only "text" for your data points in fviz_pca_biplot and this will ensure your var shape is 15. Then you call another geom_point() for your individual datapoints and specify the shape:

library(ggsci)
library(factoextra)

data("iris")

PCA.iris <- prcomp(iris[ , -5], scale=TRUE)

BiPlot<- fviz_pca_biplot(PCA.iris, 
                      geom = "text", 
                      geom.var = c("point", "text"),
                      label="var",
                      col.ind=iris$Species,       
                      invisible="quali",
                      pointshape = 15, pointsize = 3) +
                      geom_point(aes(col = Col.),shape=10)+
                      labs(colour= "Species" )+
                      scale_color_ucscgb() +
                      theme_gray() 
                 

enter image description here