[英]unable to change labels in PCA prcomp from row numbers to site names
[英]How to change labels from PCA using PRcomp to sample names
我試圖用樣本名稱而不是標准數字來標記PCA雙標圖。 我使用的代碼:
PRCOMP1 <- prcomp(~ Max + Min + Range + Average + P10 + P20 +
P50 + P100 + D10 + D20 + D50 + D100 + D500,
data = turbidity,
na.action = na.omit,
scale = TRUE
biplot(PRCOMP1, cex = 0.8, choices=c(1,2))
這提供了下面的圖 - 我知道我可以在我的數據表中用標記為Sample的列來標記這些點。
還有一種簡單的方法可以改變箭頭的顏色嗎? 任何幫助將非常感激。
您可以使用Sample列的值命名輸入數據的行:
row.names(turbidity) <- turbidity$Sample
然后,您的雙標圖上的點將標有其同源樣本名稱。
我試着舉個例子:
#creating an example data frame with 5 numeric and one character variables
mydata1 <- as.data.frame(matrix(rnorm(100, 0, 2), ncol = 5))
mydata1$sample <- c(sapply(1:20, function(i) paste("s", i, sep = "")))
#view of the df
mydata1
V1 V2 V3 V4 V5 sample
1 1.7398057 -0.8074246 0.009826488 0.58566480 3.88569625 s1
2 -1.3259889 -2.4359229 -1.258855445 2.65124987 -2.64137545 s2
3 -2.3961068 -0.3108402 -1.330362255 -0.35209302 -2.39282594 s3
這是一個20行乘6變量的數據幀
biplot(prcomp(mydata1[,-6]))
此語句將返回沒有樣本標簽的圖,只返回數字。
#naming rows of the df with the sample column value
row.names(mydata1) <- mydata1$sample
#viewing the df
head(mydata1)
V1 V2 V3 V4 V5 sample
s1 1.739806 -0.8074246 0.009826488 0.5856648 3.8856962 s1
s2 -1.325989 -2.4359229 -1.258855445 2.6512499 -2.6413755 s2
s3 -2.396107 -0.3108402 -1.330362255 -0.3520930 -2.3928259 s3
#plotting
biplot(prcomp(mydata1[,-6]))
后一個圖現在將使用其標簽呈現觀察結果。 如果這是您的想法,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.