library(PCAtools)
library(tidyverse)
ls(package:PCAtools)
iris <- as.data.frame(iris)
iris <- iris %>% mutate(class = str_c("a",1:dim(iris)[1],sep = ""))
rownames(iris) <- iris$class
iris <- iris[,-6]
head(iris)
# 构建矩阵
expr=iris[c(1,2,3,4)] # 表达矩阵,行是基因,列是样本名
head(expr)
class <- iris[5] #分组信息,行是样本名,每一列是对应的分组信息
head(class)
expr <- scale(expr)
head(expr)
expr <-t(expr) # 表达矩阵,行是基因,列是样本名
expr[,c(1:4)]
pca <- pca(expr, metadata = class)
biplot(pca,x="PC1",y="PC2",colby = "Species",
legendPosition = "right",lab = NULL,
encircle = TRUE, encircleFill = TRUE)
# pca[["variance"]]
# pca[["variance"]][["PC1"]]