加载需要用的包
library(Seurat)
library(ggplot2)
library(ggsignif)
加载函数
PlotPercent <- function(seurat.object,path.out){
meta_data_new <- seurat.object@meta.data[,match(c("orig.ident","seurat_clusters"),colnames(seurat.object@meta.data))]
plot_data_list <- split(meta_data_new,f=meta_data_new$orig.ident)
plot_data_list <- lapply(plot_data_list,function(x){x <- as.data.frame(table(x));x$percent <- x$Freq/sum(x$Freq);return(x)})
conbine_data <- data.frame()
for (i in plot_data_list) {
conbine_data <- rbind(conbine_data,i)
}
plot_data_list_2 <- split(conbine_data,f=conbine_data$seurat_clusters)
#=============添加分组信息
plot_data_list_2 <- lapply(plot_data_list_2,function(x){
x$group <- group$sample_type[match(x$orig.ident,group$orig.ident)]
return(x)
})
#==============绘制箱线图
lapply(plot_data_list_2,function(x){
assign(paste0(unique(x$seurat_clusters),"_comparision"),ggplot(x,aes(x=group,y=percent,fill=group))+geom_boxplot()+geom_jitter()+scale_fill_manual(values = c("#56B4E9", "#E69F00"))+geom_signif(comparisons = comparisons,map_signif_level=TRUE,step_increase=0.1,test = wilcox.test))
ggsave(paste0("cluster",unique(x$seurat_clusters),"_comparision.pdf"),get(paste0(unique(x$seurat_clusters),"_comparision")),path=path.out)
}
)
}
加载数据
- group:orig.ident为样本名,sample_type为对应的分组
- comparisons:需要做检验的分组
path_out_BioRad_debach_FAP <- "/data/zhaoxueya/project/gut/result/BioRad/CD_merge/debach/subset/FAP"
seurat_object_FAT <- readRDS(file=paste(path_out_BioRad_debach_FAP,"seurat_object_subset.rds", sep="/"))
group <- data.frame(orig.ident=c("CD_1_1","CD_2_1","CD_3_1","CD_1_2","CD_2_2","CD_3_2"),
sample_type=rep(c("CrF","MAT"),each=3))
comparisons <- list(c("CrF","MAT"))
PlotPercent(seurat.object=seurat_object,path.out=path_out_BioRad_debach_FAP)