火山图学习

尝试复现文章的过程中,运用生信技能树jimmy大大的搜索神技,顺利整理出了一幅看着还不错的火山图,用的包是ggplot2,分享一下,共同学习进步!
首先用dplyr包挑选作图列, mutate(v= -log10(adj.P.Val)增加一列(y轴),以case_when()将基因根据abs(logFC)>=2&adj.P.Val(FDR) < 0.01分为上调下调及稳定基因。
通过一系列函数对横坐标range进行了限定,本例子中所得x轴(abs(logFC))最大值为6,所以横轴坐标修改为了(-6,6)。
其他自主修改的参数有颜色,透明度,以及图形大小。emmmm,个人感觉渐变不太好看(可能不习惯太浅的颜色)。

if(T){ 
  library(dplyr)
  library(tidyverse)
  library(ggplot2)
  DEG_valvone <- nrDEG
  colnames(DEG_valvone)
  DEG_valvone <- DEG_valvone %>%
    select(logFC,AveExpr, adj.P.Val) %>%
    mutate(v= -log10(adj.P.Val),
           gene_type = case_when(logFC >= 2 & adj.P.Val < 0.01 ~ "up",
                                 logFC <= -2 & adj.P.Val < 0.01 ~ "down",
                                 TRUE ~ "stable")) 
##查看x坐标轴设定范围
DEG_valvone %>%
  pull(logFC) %>%
  min() %>%
  floor()   #查看x轴最小边界 -5
DEG_valvone %>%
  pull(logFC) %>%
  max() %>%
  ceiling()  #最大边界6

###设定上调下调稳定点颜色大小透明度
cols <- c("up" = "#ffad73", "down" = "#26b3ff", "stable" = "grey") 
sizes <- c("up" = 2, "down" = 2, "stable" = 1) 
alphas <- c("up" = 1, "down" = 1, "stable" = 0.5)
}
final_plot <- DEG_valvone %>%
  ggplot(aes(x = logFC,
             y = v,
             fill = gene_type,    
             size = gene_type,
             alpha = gene_type)) + 
  geom_point(shape = 21, # Specify shape and colour as fixed local parameters    
             colour = "black") + 
  geom_hline(yintercept = -log10(0.01),
             linetype = "dashed") + 
  geom_vline(xintercept = c(-2, 2),
             linetype = "dashed") +
  scale_fill_manual(values = cols) + # Modify point colour
  scale_size_manual(values = sizes) + # Modify point size
  scale_alpha_manual(values = alphas) + # Modify point transparency
  scale_x_continuous(breaks = c(seq(-6, 6, 1)),       
                     limits = c(-6, 6))+
  labs(title = "Gene expression changes in Vemurafenib versus control samples",
       x = "log2(fold change)",
       y = "-log10(adjusted P-value)",
       colour = "Expression \nchange") +
  theme_bw() + # Select theme with a white background  
  theme(panel.border = element_rect(colour = "black", fill = NA, size= 0.5),    
        panel.grid.minor = element_blank(),
        panel.grid.major = element_blank()) 
final_plot
    ggsave("valcone_plot.png",plot = final_plot,width = 200,height = 150,units ="mm" )

valcone_plot.png

你可能感兴趣的:(火山图学习)