Knowledge base

How to plot a volcano plot

Introduction

The following are the codes for drawing a volcano plot with ggplot2

Code exmaple

#Load package and read file
library(data.table)
library(ggplot2)
library(ggrepel)

setwd("/home/hxzk/project/XXX/KEGG/")

dt_pvalue <- fread("data/P_value_table.txt")

#Filter data
dt_pvalue <- dt_pvalue[!is.na(Pvalue),][, 1:12][!is.na(log2FC)][!is.infinite(log2FC)]

# Mark sinificant, positive, negagive or gene name to shown
dt_pvalue$changed <- factor(ifelse(dt_pvalue$Pvalue < 0.05 & abs(dt_pvalue$log2FC) > 0.6,
                              ifelse(dt_pvalue$log2FC > 0.6,'positive','negative'),'no'))
#dt_pvalue$selectedgene <- ifelse(dt_pvalue$gene name %in% c("BCL2L1", "TAB2", "WDR6"),dt_pvalue$gene name,NA)
ggplot(dt_pvalue,aes(log2FC,-log10(Pvalue),
                color=factor(changed),
                size=factor(changed)))+
  geom_point()+
  labs(x=expression(Log[2]*" Fold Change"),
       y=expression(-Log[10]*" (p value)"))+
  theme_classic(base_size = 15)+
  scale_color_manual(values = c('deepskyblue','grey','tomato'))+
  scale_size_manual(values = c(2,1,2))+
  theme(legend.title = element_blank(),
        legend.position = c(0.2,0.9),
        legend.background = element_rect(fill='transparent'))+
  geom_text_repel(aes(label=selectedgene), color="black",size=4,
                  box.padding=unit(0.5, "lines"),
                  point.padding=NA,
                  segment.colour = "black")+
  geom_hline(yintercept = -log10(0.05),linetype=2,cex=1)+  #add lines
  geom_vline(xintercept = c(-0.6,0.6),linetype=2,cex=1)+
  theme(axis.text= element_text(colour = "black"),
        panel.border = element_rect(size=1,fill='transparent'))
ggsave("result/volcano.pdf", width = 5, height = 4)

References

None

Original

None

Donation

Leave a Reply

Your email address will not be published. Required fields are marked *