查看原文
其他

R绘图模板——火山图(volcanoplot)!

王志山 科研后花园 2023-09-08
 

点击上方

“科研后花园”

关注我们

代码如下:

1、加载R包

rm(list=ls())#clear Global Environmentsetwd('D:\\桌面\\火山图')#设置工作路径#加载包library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(ggrepel) # Automatically Position Non-Overlapping Text Labels with'library(RColorBrewer) # ColorBrewer Paletteslibrary(grid) # The Grid Graphics Packagelibrary(scales) # Scale Functions for Visualization

2、加载数据

##读取数据df <- read.table(file="data.txt",sep="\t",header=T,check.names=FALSE)

3、数据处理

#数据分类df$group<-as.factor(ifelse(df$pvalue < 0.05 & abs(df$log2FoldChange) >= 2, ifelse(df$log2FoldChange>= 2 ,'up','down'),'NS'))#标签df$label<-ifelse(df$pvalue<0.05&abs(df$log2FoldChange)>=4,"Y","N")df$label<-ifelse(df$label == 'Y', as.character(df$gene), '')

4、绘图

####绘制火山图p <- ggplot(df, aes(log2FoldChange, -log10(pvalue),fill = group)) + geom_point(color="black",alpha=0.6, size=3,shape=21)+ theme_bw()+ theme(panel.grid=element_blank(), axis.text=element_text(color='#333c41',size=10), legend.text = element_text(color='#333c41',size=10), legend.title = element_blank(), axis.title= element_text(size=12))+ geom_vline(xintercept = c(-2, 2), lty=3,color = 'black', lwd=0.8) + #辅助线 geom_vline(xintercept = c(-4, 4), lty=3,color = 'red', lwd=0.8)+ geom_hline(yintercept = -log10(0.05), lty=3,color = 'black', lwd=0.8) + scale_fill_manual(values = c('blue','grey','red'))+ labs(title="volcanoplot", x = 'log2 fold change', y = '-log10 pvalue')+ geom_text_repel(aes(x = log2FoldChange,#标签 y = -log10(pvalue), label=label), max.overlaps = 10000, size=3, box.padding=unit(0.8,'lines'), point.padding=unit(0.8, 'lines'), segment.color='black', show.legend=FALSE)p
#背景色color <- colorRampPalette(brewer.pal(11,"BrBG"))(30)#添加背景grid.raster(alpha(color, 0.2), width = unit(1, "npc"), height = unit(1,"npc"), interpolate = T)

温馨提示

如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注我。






您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存