簡體   English   中英

PCA 雙標組個體

[英]PCA biplot group individuals

我的數據中有很多人(n = 600)。 我運行 PCA 並想創建一個變量和個體的雙圖。 我想要根據他們的貢獻着色的變量。 這些人來自兩組,我想根據兩組對點進行着色。 我附上一個小例子。

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 個因素中的一個(我不介意這是一個例子)並為它們着色不同的顏色。

部分回答;

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))

然而,在我獲得的地方,我正在失去。 我無法找到通過 contrib 使用 habillage 和 select.var 的方法作為Error: Continuous value supplied to discrete scale不斷出現。

如果您不需要圖例:

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"))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM