画一个有标签的火山图—标出显著变化的基因

参考:ggplot2绘制火山图实例

rm(list = ls())
setwd(' ')
library(ggplot2)
DEG=read.csv('marker.csv',header = T)
rownames(DEG)=DEG[,1]
DEG$significant <- as.factor(DEG$p_val<0.05 & abs(DEG$avg_logFC) >1.5)
logFC_cutoff=0.5
DEG$change = as.factor(ifelse(DEG$p_val < 0.05 & abs(DEG$avg_logFC) > logFC_cutoff, 
                              ifelse(DEG$avg_logFC > logFC_cutoff ,'UP','DOWN'),'NOT'))
DEG$label <-''
label=DEG$X[DEG$p_val < 0.05 & DEG$significant != "FALSE"]
DEG$label[DEG$p_val < 0.05 & DEG$significant != "FALSE"] = as.character(label) #这里需要转换成character

g = ggplot(data=DEG, aes(x=DEG$avg_logFC, y=-log10(DEG$p_val), color=change)) +
  geom_point(alpha=0.4, size=1.75) +
  theme_set(theme_set(theme_bw(base_size=15)))+
  xlab("log2 fold change") + ylab("-log10 p-value") +
  geom_text(aes(label = label), size = 3,vjust=-0.5, alpha=0.8) +  
  scale_colour_manual(values = c("blue", "black", "red")) ## corresponding to the levels(res$change)

g=g+geom_vline(xintercept = -0.5, linetype = "dashed", color = "grey", size = 1) +
  geom_vline(xintercept = 0.5, linetype = "dashed", color = "grey", size = 1)+
  geom_hline(yintercept = 1.3, linetype = "dashed", color = "grey", size = 1)
g

还可以添加title

rm(list = ls())
setwd(' ')
library(ggplot2)
DEG=read.csv('marker.csv',header = T)
rownames(DEG)=DEG[,1]
DEG$significant <- as.factor(DEG$p_val<0.05 & abs(DEG$avg_logFC) >1.5)

logFC_cutoff=0.5
DEG$change = as.factor(ifelse(DEG$p_val < 0.05 & abs(DEG$avg_logFC) > logFC_cutoff, 
                              ifelse(DEG$avg_logFC > logFC_cutoff ,'UP','DOWN'),'NOT'))

this_tile <- paste0('Cutoff for logFC is ',round(logFC_cutoff,3),
                    '\nThe number of up gene is ',nrow(DEG[DEG$change =='UP',]) ,
                    '\nThe number of down gene is ',nrow(DEG[DEG$change =='DOWN',]))

DEG$label <-''
label=DEG$X[DEG$p_val < 0.05 & DEG$significant != "FALSE"]
DEG$label[DEG$p_val < 0.05 & DEG$significant != "FALSE"] = as.character(label) 

g = ggplot(data=DEG, aes(x=DEG$avg_logFC, y=-log10(DEG$p_val), color=change)) +
  geom_point(alpha=0.4, size=1.75) +geom_text(aes(label = label), size = 3,vjust=-0.5, alpha=0.8) +
  theme_set(theme_set(theme_bw(base_size=13)))+
  xlab("log2 fold change") + ylab("-log10 p-value") +
  ggtitle( this_tile ) + theme(plot.title = element_text(size=15,hjust = 0.5))+
  scale_colour_manual(values = c('blue','black','red')) ## corresponding to the levels(res$change)

g=g+geom_vline(xintercept = -0.5, linetype = "dashed", color = "grey", size = 1) +
  geom_vline(xintercept = 0.5, linetype = "dashed", color = "grey", size = 1)+
  geom_hline(yintercept = 1.3, linetype = "dashed", color = "grey", size = 1)
g

用点的面积表示abs(logFC)的大小

#其他参数不变
g = ggplot(data=DEG, aes(x=DEG$avg_logFC, y=-log10(DEG$p_val), color=change,size=abs(DEG$avg_logFC))) +
  geom_point(alpha=.5)+scale_size_area(max_size = 8)+
  theme_set(theme_set(theme_bw(base_size=13)))+
  xlab("log2 fold change") + ylab("-log10 p-value") +
  geom_text(aes(label = DEG$label), size = 3,vjust=-0.5, alpha=1)+  
  scale_colour_manual(values = c("#6A5ACD", "#000000", "#FF6A6A"))

你可能感兴趣的:(画一个有标签的火山图—标出显著变化的基因)