样本数据PCA分析

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

结果如图
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值