基本函数:
sample_exp为不同年龄分组的表达矩阵
library(ggplot2)
library(ggsignif)#加载显著性包
sample_exp$age <- factor(sample_exp$age,levels=c("3m","18m","24m")) #自定义坐标轴顺序
boxplot <- ggplot(
sample_exp,
aes(x=age,y=exp,fill=age))+
geom_boxplot()+
scale_fill_manual(values = c("#56B4E9", "#E69F00","#996699"))+
geom_signif(comparisons = compare,map_signif_level=TRUE,step_increase=0.1,test = wilcox.test)
ggsave("box_plot.png",boxplot,path=path.out)
单细胞数据seurat_object:
seurat_object_list为按照某个meta.data中的因素如cell_ontology_class对seurat_object进行的分裂的列表
library(Seurat)
seurat_object_heart_facs_list <- SplitObject(seurat_object_heart_facs,split.by="cell_ontology_class")
BoxPlotforAllcelltype <- function(seurat_object,seurat_object_list,symbol,compare,path.out){
features_all <- rownames(GetAssayData(seurat_object))
if (symbol %in% features_all ){
exp <- GetAssayData(seurat_object)
cell_class <- data.frame(cell=rownames(seurat_object@meta.data),age=seurat_object@meta.data$age,exp=exp[rownames(exp)==symbol,][match(rownames(seurat_object@meta.data),names(exp[rownames(exp)==symbol,]))])
cell_class$age <- factor(cell_class$age,levels=c("3m","18m","24m")) #自定义坐标轴顺序
#整体比较
boxplot_all <- ggplot(cell_class,aes(x=age,y=exp,fill=age))+
geom_boxplot()+
scale_fill_manual(values = c("#56B4E9", "#E69F00","#996699"))+
geom_signif(comparisons = compare,map_signif_level=TRUE,step_increase=0.1,test = wilcox.test)+
theme(axis.text=element_text(size=15))
ggsave(paste(symbol,"_box_plot_all",".png",sep=""),boxplot_all,path=path.out)
#每种细胞类型单独比较
for (i in names(seurat_object_list)){
exp_i <- GetAssayData(seurat_object_list[[i]])
cell_class_i <- data.frame(cell=rownames(seurat_object_list[[i]]@meta.data),age=seurat_object_list[[i]]@meta.data$age,exp=exp_i[rownames(exp_i)==symbol,][match(rownames(seurat_object_list[[i]]@meta.data),names(exp_i[rownames(exp_i)==symbol,]))])
cell_class_i$age <- factor(cell_class_i$age,levels=c("3m","18m","24m")) #自定义坐标轴顺序
boxplot <- ggplot(cell_class_i,aes(x=age,y=exp,fill=age))+
geom_boxplot()+
scale_fill_manual(values = c("#56B4E9", "#E69F00","#996699"))+
geom_signif(comparisons = compare,map_signif_level=TRUE,step_increase=0.1,test = wilcox.test)+
theme(axis.text=element_text(size=15))
ggsave(paste(symbol,"_box_plot_",i,".png",sep=""),boxplot,path=path.out,width=15,height=20)
vlnplot <- VlnPlot(seurat_object_list[[i]],features=symbol,group.by="age")
ggsave(paste(symbol,"_vln_plot_",i,".png",sep=""),vlnplot,path=path.out)
}
}
}
BoxPlotforAllcelltype(seurat_object=seurat_object_heart_facs,seurat_object_list=seurat_object_heart_facs_list,symbol=genes,compare=comparisons_facs,path.out=path_out_heart_facs)