PCA分析及画图
library(ggpubr)
library(ggplot2)
library(ggthemes)
data1<-read.table('./100klist.txt',header = 1)[,1:4]
head(data1)
BCD_rep1 BCD_rep2 CK_rep1 CK_rep2
1 0.1987 0.2036 0.1807 0.2062
2 0.2133 0.2169 0.2040 0.2140
3 0.1943 0.1965 0.1862 0.1902
4 0.1051 0.1005 0.0928 0.1072
5 0.1627 0.1546 0.1453 0.1488
6 0.1283 0.1322 0.1123 0.1270
pca.info <- prcomp(data1,scale=F)
#显示PCA计算结果
head(pca.info$rotation)
percentage<-round(pca.info$sdev / sum(pca.info$sdev) * 100,2)
# df_pcs <-data.frame(pca.info$x, Species = iris$Species)
percentage<-paste(c("PC1","PC2"),"(", paste(as.character(percentage), "%", ")", sep=""))
pca.data <- data.frame(sample = rownames(pca.info$rotation),
# type=c(rep("0h",3),rep("0.5h",3),rep("1h",3),rep("2h",3),rep("4h",3),rep("8h",3),
# rep("0h",3),rep("0.5h",3),rep("1h",3),rep("2h",3),rep("4h",3),rep("8h",3)),
Sample = c(rep("BCD",2),rep("CK",2)),
pca.info$rotation) #sample 设置形状,type设置颜色
#绘图
table(pca.data$Sample)
pca.data$Sample<- factor(pca.data$Sample,
levels= c('BCD','CK'),ordered = TRUE)
ggscatter(pca.data,x = "PC1",y = "PC2",
label = "sample",color = "Sample",ellipse = F ,size = 4) +#,shape = "Sample"
theme_bw()+
xlab(percentage[1]) + ylab(percentage[2])+
scale_color_brewer(type="seq",palette="Set1")+
# guides(color = guide_legend(title = ''))+
theme(axis.title = element_text(face = "bold",
size = "19",color = "black"),
axis.text.x = element_text(face = "bold",color = "black",angle = 0,
size = 15, hjust = 0.5, vjust = 0.5),
axis.text.y = element_text(face = "bold",size = 15,color = "black"),
# strip.text.y = element_text(face = 'bold',size = 10,colour = 'white'),####设置分面的的title框的形状\字体
# strip.text.x = element_text(face = 'bold',size = 10,colour = 'white'),
strip.text.x = element_text( face="bold",size = 16),
#size=8,angle=75
legend.text = element_text(size = 14,face = "bold"),
legend.title = element_text(size = 14,
face = "bold"),
legend.position ='top',
strip.text.y = element_text( face="bold",size = 16),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.border = element_rect(colour = "black", fill=NA, size=1.5),
strip.background = element_rect(colour = "white", fill = "grey"))
结果如图