火山图

分析背景

在前段时间,小编给各位小伙伴们介绍了使用ggplot2包绘制差异基因火山图,私底下有小姐姐向小编反应说图形不够美观,毕竟这个这是个看脸的时代,今天小编给小伙伴们介绍使用ggpubr包绘制带有自定义有颜色的标签,如下图所示,是不是耳目一新。

ggpubr是由Alboukadel Kassambara基于ggplot2、ggsci包开发的,用于绘制符合出版物要求的图形。该包封装了很多ggplot2的绘图函数,并且内嵌了ggsci中很多优秀的学术期刊配色方案,值得学习使用。 ggpubr包括一些关键的特性:

  • 能帮助研究人员快速创建易于发表的图形

  • 能够将P值和显著性水平自动添加到图形上而无需二次编辑

  • 使图形注释和排版变得容易

  • 使更改图形参数(例如颜色和标签)变得容易

小编,在后期使用这个包给大家绘制各种常见的图形,如小提琴图、箱线图、散点图等等。记得关注公众号,干货满满!!!

分析方法
# 安装R包
if (!requireNamespace("ggplot2", quietly = TRUE))
  install.packages("ggplot2",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
if (!requireNamespace("RColorBrewer", quietly = TRUE))
  install.packages("RColorBrewer",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
if (!requireNamespace("data.table", quietly = TRUE))
  install.packages("data.table",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
if (!requireNamespace("ggpubr", quietly = TRUE))
  install.packages("ggpubr",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
if (!requireNamespace("tidyselect", quietly = TRUE))
  install.packages("tidyselect",repos = "https://mirrors.tuna.tsinghua.edu.cn/CRAN/")
# 自定义函数
## 快速读入数据
readFlie=function(input,type,row=T,header=T){
  # input 为读入文件的路径,type为读入文件的类型,格式为‘.txt’或‘.csv’,row=T,将文件的第一列设置为列名
  library(data.table,quietly = TRUE)
  if(type=='txt'){
    dat = fread(input,header = header,sep='\t',stringsAsFactors = F,check.names = F)
    if(row){
      dat = as.data.frame(dat,stringsAsFactors = F)
      rownames(dat) = dat[,1]
      dat = dat[,-1]
    }else{
      dat = as.data.frame(dat,stringsAsFactors = F)
    }
  }else{
    dat = fread(input,header = header,sep=',',stringsAsFactors = F,check.names = F)
    if(row){
      dat = as.data.frame(dat,stringsAsFactors = F)
      rownames(dat) = dat[,1]
      dat = dat[,-1]
    }else{
      dat = as.data.frame(dat,stringsAsFactors = F)
    }
  }
  return(dat)
}
## 保存图片,只支持ggplot对象
savePlots=function(path,plot,type=c('pdf','png','tiff')[1],width=10,height=8,dpi=300){
  # path表示保存图片路径,需要加上相应的文件扩展名称
  library(ggplot2)
  if(type=='pdf'){
    ggsave(filename = path,plot = plot,width = width,height = height,device = 'pdf')
  }else if(type=='png'){
    ggsave(filename = path,plot = plot,width = width,height = height,device = 'png',dpi = dpi)
  }else{
    ggsave(filename = path,plot = plot,width = width,height = height,device = 'tiff',dpi = dpi)
  }
}

 

阅读原文,获取脚本源码和测试数据。

实战演练
# 实战演练
## 读入数据
df = readFlie('./volcano_file.txt',type = 'txt',row = T ) 
## 绘图
fg = wnb_volcano(df)
## 展示图形
fg
## 保存图形
# 保存图片
savePlots(path = './fg.pdf',plot = fg,type = 'pdf',width = 10,height = 8)
savePlots(path = './fg.png',plot = fg,type = 'png',width = 10,height = 8,dpi = 300)
savePlots(path = './fg.tiff',plot = fg,type = 'tiff',width = 10,height = 8,dpi = 600)
​
fg1 = wnb_volcano(df,colors =  c("#00AFBB", "#999999", "#FC4E07"),
                  showGene = c('MUC4','SCIN','TTC9','CLIC6','GSTA3','ATP12A','MUC16','CLDN10','KCNE1','AK7'))
## 展示图形
fg1
## 保存图形
# 保存图片
savePlots(path = './fg1.pdf',plot = fg1,type = 'pdf',width = 10,height = 8)
savePlots(path = './fg1.png',plot = fg1,type = 'png',width = 10,height = 8,dpi = 300)
savePlots(path = './fg1.tiff',plot = fg1,type = 'tiff',width = 10,height = 8,dpi = 600)

 

联系我们:

小伙伴们对脚本有疑问或者想加入我们生信学习福利群,扫描下方二维码,加入我们  :

 

posted @ 2020-09-10 10:30  稳我  阅读(344)  评论(0编辑  收藏  举报