How does R know where to place the confidence ellipse for a PCA plot? I have a minimal code using the iris dataset:
library(factoextra)
a<-data.matrix(iris[-5])
b<-prcomp(a, scale. = TRUE, center = TRUE)
fviz_pca_ind(b,
col.ind = iris$Species,
addEllipses = TRUE)
I know that I can find the plot coordinate with b$x. I also know that I can find the cluster centers with b$center. How do I re-derive the ellipses from the data?
If you are talking about the how, it eventually calls ggplot2::stat_ellipse.
If you want the coordinates, like with other ggplot objects, you can extract the data with ggplot_build
library(factoextra)
a<-data.matrix(iris[-5])
b<-prcomp(a, scale. = TRUE, center = TRUE)
p <- fviz_pca_ind(b,
col.ind = iris$Species,
addEllipses = TRUE)
ell <- ggplot2::ggplot_build(p)$data[[2]]
head(ell)
# colour fill x y group PANEL size linetype alpha
# 1 #F8766D #F8766D -1.697756 -0.06395559 1 1 0.5 1 0.1
# 2 #F8766D #F8766D -1.701694 0.22197334 1 1 0.5 1 0.1
# 3 #F8766D #F8766D -1.713449 0.50017215 1 1 0.5 1 0.1
# 4 #F8766D #F8766D -1.732842 0.76642364 1 1 0.5 1 0.1
# 5 #F8766D #F8766D -1.759579 1.01669171 1 1 0.5 1 0.1
# 6 #F8766D #F8766D -1.793255 1.24718254 1 1 0.5 1 0.1
p + geom_point(aes(x, y, color = factor(group)), data = ell, size = 4)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With