RNA-seq

R-韦恩图系列-VennDiagram

2021-11-01  本文已影响0人  小贝学生信

R-韦恩图系列-ggVennDiagram - 简书 (jianshu.com)
R-韦恩图系列-VennDiagram - 简书 (jianshu.com)
R-韦恩图系列-UpSetR - 简书 (jianshu.com)

VennDiagram包是绘制韦恩图的一个经典包了。它不是基于ggplot体系,但可以通过转换变为ggplot对象(我更习惯些)。另外每次绘图时,但会生成一个日志log文件,比较麻烦。
参考教程:https://www.r-graph-gallery.com/14-venn-diagramm.html

# install.packages("VennDiagram")
library(VennDiagram)

genes <- paste0("gene",1:1000)
set.seed(20210302)
gene_list <- list(A = sample(genes,100),
                  B = sample(genes,200),
                  C = sample(genes,300),
                  D = sample(genes,200))
#保存图片至本地
venn.diagram(gene_list, filename = "test.png")

#在R界面显示
venn.plot = venn.diagram(gene_list, 
                         filename = NULL)
grid.draw(venn.plot)
file.remove(list.files(pattern = "*log"))
dev.off()

1、类别名 category name

category.names, #类别名内容
cat.cex = 0.6,  #类别名字体大小(相对1)
cat.fontface = "bold", #加粗
cat.col = rainbow(3)
cat.pos = c(-27, 27, 135),   #0度为12点方向,顺时针旋转
cat.dist = c(0.055, 0.055, 0.085), #类别名距离圆的距离
cat.fontfamily = "sans", #字体
rotation.degree = 60 #旋转角度(逆时针)
rotation = 1 #适合3 set的情况
venn.plot = venn.diagram(gene_list[1:3], filename = NULL,
                         category.names = LETTERS[1:3],
                         cat.cex = 1.5,
                         cat.fontface = "bold",
                         cat.pos = c(-20, 20, 180),
                         cat.col = rainbow(3))
grid.draw(venn.plot)
file.remove(list.files(pattern = "*log"))
dev.off()

2、圆的相关设置

fill  #填充颜色
col # 边的颜色
lwd = 2, #边的宽度
lty = 'blank', #边的类型 dashed dotted
library(RColorBrewer)
venn.plot = venn.diagram(gene_list, filename = NULL,
                         fill = brewer.pal(4, "Pastel2"),
                         col = brewer.pal(4, "Pastel2"),
                         lwd = 2, lty = "dashed")
grid.draw(venn.plot)
file.remove(list.files(pattern = "*log"))
dev.off()

如上图,我认为VennDiagramggVennDiagram包最大的不同就在于圆的填充颜色:前者为离散的颜色,而后者为连续渐变颜色(参看上一篇笔记)

3、交集数值展示的相关设置

venn.plot = venn.diagram(gene_list,
                         cex = 2,
                         fontface = "bold", 
                         filename = NULL)
grid.draw(venn.plot)
file.remove(list.files(pattern = "*log"))
dev.off()

4、特殊情况 2 sets

gene_list2 <- list(A = sample(genes,20),
                  B = sample(genes,100))
venn.plot = venn.diagram(gene_list2,
                         filename = NULL)
grid.draw(venn.plot)
file.remove(list.files(pattern = "*log"))
dev.off()
# ext.text = F
# ext.percent = 0 重叠数目低于多少百分比,使用线
# ext.pos
# ext.line.lwd
venn.plot = venn.diagram(gene_list2,
                         ext.text = F,
                         filename = NULL)
grid.draw(venn.plot)
file.remove(list.files(pattern = "*log"))
dev.off()
venn.plot <- draw.pairwise.venn(
  area1 = 50,
  area2 = 50,
  cross.area = 20)
dev.off()

#修改展示标签内容
venn.plot[[5]]$label = length(setdiff(gene_list2[[1]], gene_list2[[2]]))
venn.plot[[6]]$label = length(setdiff(gene_list2[[2]], gene_list2[[1]]))
venn.plot[[7]]$label = length(intersect(gene_list2[[1]], gene_list2[[2]]))
grid.draw(venn.plot)
file.remove(list.files(pattern = "*log"))
dev.off()

5、转为ggplot2对象

library(ggplot2)
library(ggplotify)
library(cowplot)
as.ggplot(plot_grid(grobTree(venn.plot))) +
  ggtitle("This is title")

最后关于韦恩图的标题,venn.diagram函数的mainsub系列参数可以设置;另外由于可以转换为ggplot对象,所以之后使用ggplot title相关函数也可以~

上一篇 下一篇

猜你喜欢

热点阅读