【问题标题】:PCA biplot group individualsPCA 双图组个体
【发布时间】:2016-05-16 02:02:14
【问题描述】:

我的数据中有很多人 (n=600)。我运行 PCA 并想创建变量和个体的 Biplot。我想要由它们的贡献着色的变量。这些人来自两组,我想根据两组对点进行着色。我附上一个小例子。

library(FactoMineR)
library(factoextra)
data(decathlon2)
decathlon2.active <- decathlon2[1:23, 1:10]
head(decathlon2.active[, 1:6])
res.pca <- PCA(decathlon2.active, graph = FALSE)
fviz_pca_biplot(res.pca, col.var="cos", geom = "point") + scale_color_gradient2(low="white", mid="blue", 
                    high="red", midpoint=0.5) + theme_minimal()

res.pca_ind = data.frame(res.pca$ind)
res.pca_ind

问题;

  1. 如何将行名 SEBRLE 和 NOOL 涂成红色,其余部分涂成黑色
  2. 将所有行名分配给 2 个因素中的 1 个(我不介意这只是一个示例)并为它们着色。

部分回答;

sub = as.character(rownames(res.pca_ind))
decathlon3 = decathlon2[which(rownames(decathlon2) %in% sub),]

fviz_pca_biplot(res.pca, axes = c(1, 2), geom = c("point", "text"),
            label = "all", invisible = "none", labelsize = 2, pointsize = 2,
            habillage = decathlon3$Competition, addEllipses = FALSE,    ellipse.level = 0.95,
            col.ind = "black", col.ind.sup = "blue", alpha.ind = 1,
            col.var = "steelblue", alpha.var = 1, col.quanti.sup = "blue",
            col.circle = NULL, 
            select.var = list(name = NULL, cos2 = NULL, contrib= NULL), 
            select.ind = list(name = NULL, cos2 = NULL, contrib = NULL),
            jitter = list(what = "label", width = NULL, height = NULL))

然而,在我得到的地方我正在失去。由于Error: Continuous value supplied to discrete scale 不断出现,我无法找到同时使用habillage 和contrib 的select.var 的方法。

【问题讨论】:

    标签: r pca factoextra


    【解决方案1】:

    如果您不需要图例:

    library(factoextra)
    
    point_col = ifelse(rownames(decathlon2.active) %in% c("SEBRLE","NOOL"),
    "red","black")
    
    res.pca <- PCA(decathlon2.active, graph = FALSE)
    g = fviz_pca_biplot(res.pca, col.var="contrib", geom = "") +
    scale_color_gradient2(low="white", mid="blue", high="red", midpoint=0.5) + 
    theme_minimal() 
    g + geom_point(col = point_col)
    

    如果你需要一个图例,我将包含 sebrle 和 nool 的观察命名为 0,其他命名为 1:

    library(ggnewscale)
    
    g$data$group = +(rownames(decathlon2.active) %in% c("SEBRLE","NOOL"))
    g + new_scale_color() + 
    geom_point(aes(col=factor(group))) + 
    scale_color_manual(values = c("black","red"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 2022-12-12
      • 2017-01-21
      • 1970-01-01
      • 2020-01-22
      相关资源
      最近更新 更多