2
votes

G'day. I am plotting a pca with the factoextra package. I have 3 points for each factor and would like to draw ellipses around each. But I am getting the error Too few points to calculate an ellipse.

It is possible to draw ellipses around 3 points in ggplot2 with the stat_ellipse function. I can confirm this by looking at the calculate_ellipse code from ggplot2 that says else if (dfd < 3) {message("Too few points to calculate an ellipse"). So what ellipse function is factoextra using in fviz_pca_ind that it considers 3 points too few? Is there a way I can force it to add ellipses? This package has specific features I need so would like to stick with it. Thanks.

library(factoextra)

data(iris)

iris2<-iris[c(1:3,51:53,101:103),] # 3 points for each factor

res.pca <- prcomp(iris2[, -5],  scale = TRUE)

fviz_pca_ind(res.pca, label='none',alpha.ind = 1,
             habillage=iris2$Species,
             repel = TRUE, 
             addEllipses = TRUE,invisible='quali')+
  theme(legend.position = 'bottom')+
  coord_equal()

#Too few points to calculate an ellipse
#Too few points to calculate an ellipse
#Too few points to calculate an ellipse

enter image description here

1
You're trying to calculate an uncertainty ellipse in 2-parameter space. An ellipse is uniquely characterised by three different points, with no uncertainty. You therefore need >3 points to be able to show an uncertainty ellipse; in other words an uncertainty ellipse with 3 points is a contradiction.Maurits Evers
@MauritsEvers thanks for the comment. Can you please elaborate? Do you mean this does not calculate confidence interval ellipses?J.Con
Imagine fitting a straight line through 2 points. In that case it doesn't make sense to talk about an uncertainty of the fit parameters. There is no uncertainty, because there is a unique line passing through 2 points.Maurits Evers
Perhaps also of interest: Real meaning of confidence ellipse.Maurits Evers
@MauritsEvers very intersting, thanks. I guess I am still confused as to why it is possible to draw ellipses with 3 points in ggplot2 but not factoextra.J.Con

1 Answers

1
votes

I've faced the same problem. The solution is to use geom_mark_ellipse from ggforce package. It's possible to create an ellipse around 3 points (even around 1 point).

So, the workflow should be as follows:

library(factoextra)
library(ggforce)

data(iris)

iris2<-iris[c(1:3,51:53,101:103),] # 3 points for each factor

res.pca <- prcomp(iris2[, -5],  scale = TRUE)

fviz_pca_ind(res.pca, label='none',alpha.ind = 1,
             habillage=iris2$Species,
             repel = TRUE, 
             # Don't use default Ellipses!!!!
             # addEllipses = TRUE,
             invisible='quali') +
  # ADD ggforce's ellipses
  ggforce::geom_mark_ellipse(aes(fill = Groups,
                        color = Groups)) +
  theme(legend.position = 'bottom') +
  coord_equal()

enter image description here