火山图美化神器——ggpubr包
2020-9-22 08:31
阅读:2920
在前段时间,小编给各位小伙伴们介绍了使用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)
转载本文请联系原作者获取授权,同时请注明本文来自李娟科学网博客。
链接地址:https://wap.sciencenet.cn/blog-3445347-1251587.html?mobile=1
收藏
当前推荐数:0
推荐到博客首页
网友评论0 条评论