Adding labels to ellipses in a PCA in r

Viewed 30

¿Is there some way to add labels to ellipses in a PCA?

E.g. I would like to add labels to these ellipses enter image description here

like that:

enter image description here (See the bold and big words over each ellipse, for this example I added labels with Photoshop)

Code:

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

fviz_pca_biplot(res.pca, label = "var", habillage=iris$Species,
                addEllipses=TRUE, ellipse.level=0.95,
                ggtheme = theme_minimal())

In the "label" argument there is not an option to put labels on the ellipses, and I do not have any idea how to add labels over each ellipse.

I will be very grateful with some direction

The script come from https://rpkgs.datanovia.com/factoextra/reference/fviz_pca.html

1 Answers

You can add a geom_text layer:

library(factoextra)
library(tidyverse)

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

fviz_pca_biplot(res.pca, 
                label = "var", 
                habillage = iris$Species,
                addEllipses = TRUE, 
                ellipse.level = 0.95,
                ggtheme = theme_minimal(base_size = 16)) + 
  geom_text(data     = . %>% 
                       group_by(Groups) %>% 
                       summarise(x = mean(x), y = mean(y)),
            mapping  = aes(label = Groups), 
            size     = 8, 
            fontface = 2, 
            color    = c("red4", "green4", "navy"),
            nudge_x  = c(-0.1, -0.5, 0.6),
            nudge_y  = c(-0.1, 1.5, -1.3))

enter image description here

Related