加载包
library(Seurat)
library(ggplot2)
library(openxlsx)
library(ggsignif)#加载显著性包
group为metadata中的某个colname,按照某个因素进行比较
Plot4Feature <- function(seurat_object.list,gene,group,comparisons,path.out){
for (i in names(seurat_object.list)){
plot_Vln <- VlnPlot(seurat_object.list[[i]],features=gene,group.by=group)
ggsave(paste0("vln_plot_",i,"_",gene,".pdf"),plot_Vln,path=path.out)
plot_data <- plot_Vln$data
plot_box <- ggplot(plot_data,aes(x=ident,y=get(gene),fill=ident))+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("box_plot_",gene,"_",i,".pdf"),plot_box,path=path.out)
}
}
comparisons为上述选择因素中因子的不同水平,比如"condition"这个因素下,对"CL","saline"这两个水平进行比较:
comparisons <- list(c("CL","saline"))
Plot4Feature(seurat_object.list=list(all=seurat_object_filter_cluster_0.5),gene="Ephx2",group="condition",comparisons=comparisons,path.out=path_out)
Plot4Feature(seurat_object.list=seurat_object_filter_cluster_0.5_cell_type_list,gene="Ephx2",group="condition",comparisons=comparisons,path.out=path_out)
注:seurat_object.list必须是list的形式