【问题标题】:color only mean group of PCA颜色仅表示 PCA 组
【发布时间】:2020-05-31 17:13:55
【问题描述】:

假设我有一个 PCA(使用 FactoMineR 的 prcomp() 或 PCA()。我想将每组的所有点绘制为黑色,但只标记和着色每组的平均点。例如,

library(FactoMineR)
library(factoextra)
iris
res.ACP<-PCA(iris[,c(1:3)],scale.unit = TRUE,ncp= 5)
fviz_pca_ind(res.ACP,geom.ind=c("point"),point.size=3,pointshape=16,
             col.ind=iris$Species,
             col="Set1",legend.title="Species",addlabel=TRUE, mean.point.size=5)

如何保持上述平均点的颜色,但将所有数据点绘制为黑色?理想情况下,我还想标记手段,然后我会放弃图例。

感谢您的帮助。

【问题讨论】:

    标签: r ggplot2 plot pca


    【解决方案1】:

    这是一个可能的解决方案。

    library(FactoMineR)
    library(factoextra)
    library(dplyr)
    
    res.ACP <- PCA(iris[,c(1:3)], scale.unit = TRUE, ncp= 5)
    
    df <- data.frame(res.ACP$ind$coord[,1:2], Species=iris$Species)
    df <- df %>%
          group_by(Species) %>%
          summarize(Dim1=mean(Dim.1), Dim2=mean(Dim.2))
    
    fviz_pca_ind(res.ACP, geom.ind=c("point"), point.size=3, pointshape=16,
                 legend.title="Species", addlabel=TRUE) +
      geom_text(data=df, aes(x=Dim1, y=Dim2, label=Species, color=Species), 
                size=6, nudge_y=0.25) +
      geom_point(data=df, aes(x=Dim1, y=Dim2, color=Species), size=5)
    

    【讨论】:

    • 这太棒了。现在我可以在创建的 df 对象上使用 dplyr::filter 进一步显示哪些意思点。谢谢!
    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-07
    相关资源
    最近更新 更多