Adding ellipses to a principal component analysis (PCA) plot

后端 未结 3 1269
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 21:11

I am having trouble adding grouping variable ellipses on top of an individual site PCA factor plot which also includes PCA variable factor arrows.

My code:



        
3条回答
  •  感动是毒
    2020-12-02 21:51

    Here's a ggplot solution, using the nice ggbiplot library. An obvious improvement over plot are the labels on this one.

    library(devtools) # don't forget to install Rtools first
    install_github("vqv/ggbiplot")
    
    library(ggbiplot)
    data.class <- data[,1]
    data.pca <- prcomp(data[,2:9], scale. = TRUE)
    g <- ggbiplot(data.pca, obs.scale = 1, var.scale = 1, 
                  groups = data.class, ellipse = TRUE, circle = TRUE)
    g <- g + scale_color_discrete(name = '')
    g <- g + theme(legend.direction = 'horizontal', 
                  legend.position = 'top')
    print(g)
    

    enter image description here

提交回复
热议问题