rggplot2pcaggfortify

Draw only 2 ellipses in PCA plot (instead of 20)


I have a PCA plot created with ggplot/ggfortify and the function autoplot(), such as in this question: Change point colors and color of frame/ellipse around points

head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
df <- iris[c(1, 2, 3, 4)]
autoplot(prcomp(df))
autoplot(prcomp(df), data = iris, colour = 'Species')
autoplot(prcomp(df), data = iris, colour = 'Species', shape='Species', frame=T) 

Is there a way to draw only 1 or 2 frames/ellipses, instead of all of them, in the PCA plot?


Solution

  • The problem with using autoplot is that, although it is great for producing nice visualizations of common data structures and models with little effort, it doesn't give you the full freedom to customize the plot. However, it is pretty straightforward to do the whole thing within ggplot. The following is a full reprex:

    library(ggplot2)
    
    pc <- prcomp(iris[1:4])
    df <- cbind(pc$x[,1:2], iris)
    
    ggplot(df, aes(PC1, PC2, color = Species)) + 
      geom_point() +
      stat_ellipse(geom = "polygon", aes(fill = after_scale(alpha(colour, 0.3))),
                   data = df[df$Species != "versicolor",])
    

    enter image description here

    Created on 2022-02-21 by the reprex package (v2.0.1)