library(ggrepel)
VolcanoPlot <- function(diff.genes,p.cutoff,logFC.cutoff,path.out,tag){
#只取表达变化前10的基因进行标注,所以其余的都设置为NA
diff.genes <- diff.genes[order(diff.genes$pvalue),]
diff.genes$anno_name <- diff.genes$symbol
diff.genes$anno_name[11:nrow(diff.genes)] <- NA
diff.genes$Change <- ifelse(diff.genes$log2FoldChange > logFC.cutoff & diff.genes$pvalue < p.cutoff, "Up", ifelse(diff.genes$log2FoldChange < -logFC.cutoff & diff.genes$pvalue < p.cutoff, "Down","No"))
title <- paste(tag,
'\ncutoff for log2FoldChange is',logFC.cutoff,
'\ncutoff for pvalue is ' ,p.cutoff,
'\nthe number of up gene is ',nrow(diff.genes[diff.genes$Change=='Up',]),
'\nthe number of down gene is ',nrow(diff.genes[diff.genes$Change=='Down',]))
print(title)
plot <- ggplot(data = diff.genes, aes(x = log2FoldChange, y = -log10(pvalue),colour = Change))+
geom_point(alpha = 0.5)+
scale_colour_manual(values = c('blue','grey10','red'))+
geom_text_repel(aes(label=anno_name),show.legend = F,segment.colour = 'black')+
xlab('log2FoldChange')+ylab('-log10(pvalue)')+
ggtitle(title)+
geom_hline(yintercept = -log10(p.cutoff),lty=4,lwd=0.6,alpha=0.8)+
geom_vline(xintercept = c(logFC.cutoff,-logFC.cutoff),lty=4,lwd=0.6,alpha=0.8)+
theme_bw()+
theme(panel.border = element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),axis.line = element_line(colour = "black"))
ggsave(paste("volcano_",tag,".png",sep=""),plot,path=path.out)
}
导入数据
for (tissue in names(DESeq2_all_sample)){
VolcanoPlot(diff.genes=DESeq2_all_sample[[tissue]],p.cutoff=0.05,logFC.cutoff=0,path.out=path_out,tag=tissue)
}