绝世美图

【傻瓜美图】PCA

2023-07-07  本文已影响0人  pudding815
image.png

1、数据就是把FPKM读进去
行名是基因,列名是样品


1688776895536.png

2、对样品分组,第一列是样品,第二列是直径


1688776868098.png
data <- read.csv("uniqueFPKM.csv",sep = ",",header = T,check.names = F,row.names = 1)
rownames(data) <- data$gene
data <- data[,-1]
library(FactoMineR)
library(factoextra)
library(ggplot2)
PCAdata <- t(data)
gene.pca <- PCA(PCAdata, 
                ncp = 2, #指定主成分分析(PCA)的主成分数目
                scale.unit = TRUE,#是否对数据进行单位缩放
                graph = FALSE)#是否绘制PCA结果的图形,默认为TRUE

plot(gene.pca) #PCA 简图

group <- read.csv("group.txt",header = T,sep = "\t")
sizes <- abs(group[,2])
type  <- as.vector(group[,1])


fviz_pca_ind(gene.pca,
             col.ind=type,
             mean.point=F,
             geom ="point",
             pointshape=1, # 点形状
             pointsize=sizes,# 点大小
             #addEllipses = T, #置信圈
             #ellipse.type="confidence",
             #ellipse.level=0.9,
             legend.title="Type",
             palette = c("navy", "firebrick"))+ #Cell配色哦
  theme(panel.border = element_rect(fill=NA,color="black", size=1, linetype="solid"))+#加个边框
  scale_size(range = c(2, 6))


  

fviz_pca_ind(gene.pca,
             col.ind=type,
             mean.point=F,
             geom ="point",
             pointshape=1, # 点形状
             pointsize=sizes,# 点大小
             #addEllipses = T, #置信圈
             #ellipse.type="confidence",
             #ellipse.level=0.9,
             legend.title="Type",
             palette = c("navy", "firebrick"))+ #Cell配色哦
  theme(panel.border = element_rect(fill=NA,color="black", size=1, linetype="solid"))+#加个边框
  scale_size(range = c(2, 6))
image.png
####-----markergene-------
ZP2 <- data["ZP2",1:151]
ZP2_value<- c(t(ZP2))


fviz_pca_ind(gene.pca,
             mean.point=F,
             geom ="point",
             col.ind = ZP2_value,
             pointshape=16, # 点形状
             legend.title="Expression",
             pointsize=3)+# 点大小
  scale_color_continuous(low = "gray", high = "red")+
  theme(panel.border = element_rect(fill=NA,color="black", size=1, linetype="solid"))+
  theme(panel.grid = element_blank())+
  ggtitle("ZP2")+
  theme(plot.title = element_text(hjust = 0.5))+
  xlab("PC1")+theme(axis.title.x = element_text(hjust = 0.5))+
  ylab("PC2")+theme(axis.title.x = element_text(hjust = 0.5))
上一篇下一篇

猜你喜欢

热点阅读