美文网首页ggplot集锦
差异分析火山图

差异分析火山图

作者: yingyonghui | 来源:发表于2022-04-29 16:33 被阅读0次
library(ggplot2)
library(ggrepel)
rm(list=ls())
options(stringsAsFactors=F)

resDat <- read.csv("xxx.csv",row.names=1,header=T,stringsAsFactors=F)
resDat <- resDat[which(!is.na(resDat$padj)), ]

resDat$logpadj <- -log10(resDat$padj)
resDat$logpadj[which(resDat$logpadj > 50)] <- 50

resDat$type <- 'NonSig'
resDat$type[which((resDat$padj < 0.01) & (resDat$log2FoldChange > 2))] <- 'Up'
resDat$type[which((resDat$padj < 0.01) & (resDat$log2FoldChange < -2))] <- 'Down'

### gene text annotation
resDat.label.up <- resDat[resDat$type=='Up',]
resDat.label.up <- resDat.label.up[order(resDat.label.up$log2FoldChange,  decreasing=T),][1:10, ]
resDat.label.up$label <- resDat.label.up$symbol
resDat.label.dn <- resDat[resDat$type=='Down',]
resDat.label.dn <- resDat.label.dn[order(resDat.label.dn$log2FoldChange,  decreasing=F),][1:10, ]
resDat.label.dn$label <- resDat.label.dn$symbol
resDat.label.dat <- rbind(resDat.label.up, resDat.label.dn)

# check the number of DEG
table(resDat$type)
# limits of x axis
ax.limit <- ceiling(max(abs(resDat$log2FoldChange)))

pdf('volcano.pdf',width=6,height=4)
ggplot(resDat,aes(x = log2FoldChange, y = logpadj)) +
    geom_point(aes(color = type), alpha = 0.5, size = 2) + 
    labs(x='log2FoldChange', y='-log10(p-adj)', title=select.group) +
    scale_color_manual(name = '', values = c('Up'='#DA1212','NonSig'='grey','Down'='#3E7C17'), 
        label = c('Up'='Up (151)','NonSig'='NonSig (17300)','Down'='Down (218)')) + 
    geom_hline(yintercept = -log10(0.01), lty = 'dashed', size = 0.5) +
    geom_vline(xintercept = c(-2,2),lty = 'dashed', size = 0.5) + 
    scale_x_continuous(limits=c(-ax.limit,ax.limit)) + 
    scale_y_continuous(limits=c(0,51)) + 
    geom_text_repel(data = resDat.label.dat, 
         aes(x = log2FoldChange, y = logpadj,  label = label), 
         size = 3, box.padding = 0.5, point.padding = 0.4,min.segment.length = 0.5,
         segment.color = "black", segment.size=0.5, show.legend = F) + 
    theme(aspect.ratio=1, 
         plot.title=element_text(hjust = 0.5, size=12), 
         axis.text = element_text(color = 'black', size=12), 
         axis.title = element_text(color = 'black',size=12),
         panel.background=element_rect(fill=NA), 
         panel.border =element_rect(fill=NA, colour = "black", size=1), 
         legend.key = element_blank())
dev.off()
WX20220429-163715@2x.png

相关文章

网友评论

    本文标题:差异分析火山图

    本文链接:https://www.haomeiwen.com/subject/nfvsyrtx.html