R语言:VennDiagram绘制venn图
2020-09-06 本文已影响0人
胡童远
导读
使用VennDiagram函数包中的venn.diagram函数绘制三数据集venn图。
安装依赖包:
# venn图
install.packages("VennDiagram")
library(VennDiagram)
一、模拟输入
产生3个含200字符串的数据集
set1 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
set2 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
set3 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
set4 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
set5 <- paste(rep("word_" , 200) , sample(c(1:1000) , 200 , replace=F) , sep="")
sample(x, size, replace=F) # 不放回抽样
二、venn基础图
1 两个圈
venn.diagram(
x = list(set1, set2),
category.names = c("Set 1" , "Set 2 "),
filename = 'venn.png',
output=TRUE
)
2 三个圈
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'venn.png',
output=TRUE
)
3 四个圈
venn.diagram(
x = list(set1, set2, set3, set4),
category.names = c("Set 1" , "Set 2 " , "Set 3", "Set 4"),
filename = 'venn.png',
output=TRUE
)
4 五个圈
venn.diagram(
x = list(set1, set2, set3, set4, set5),
category.names = c("Set 1" , "Set 2 " , "Set 3", "Set 4", "Set 5"),
filename = 'venn.png',
output=TRUE
)
三、个性化参数调整
library(RColorBrewer)
color <- brewer.pal(3, "Set3")
# Chart
venn.diagram(
x = list(set1, set2, set3),
category.names = c("Set 1" , "Set 2 " , "Set 3"),
filename = 'venn2.png',
output=TRUE,
# 输出
imagetype="png" , # 类型(tiff png svg)
#height = 1000 , # 高度
#width = 1000 , # 宽度
resolution = 400, # 分辨率
compression = "lzw", # 压缩算法
# 圈
lwd = 5, # 圈线条粗细 1 2 3 4 5
lty = 1, # 线条类型, 1 实线, 2 虚线, blank 无线条
fill = color, # 填充色
col = c("red", 'green', 'blue'), # 线条色
# 数字 number
cex = 2, # 数字大小
fontface = "bold", # 加粗
fontfamily = "sans", # 字体
# 标签 category
cat.cex = 2, # 字体大小
cat.col = c("red", 'green', 'blue'), # 字体色
cat.fontface = "bold", # 加粗
cat.default.pos = "outer", # 位置, outer 内 text 外
cat.pos = c(-27, 27, 135), # 位置,用圆的度数
cat.dist = c(0.055, 0.055, 0.085), # 位置,离圆的距离
cat.fontfamily = "sans", # 字体
rotation = 1 # 1 2 3 旋转确定大打头数据集
)
四、保存
1 绘图待保存
library(RColorBrewer)
p = venn.diagram(
x = list(set1, set2, set3, set4, set5),
category.names = c("Set 1" , "Set 2 " , "Set 3", "Set 4", "Set 5"),
# filename = 'venn.png',
filename = NULL,
output=TRUE,
fill = brewer.pal(5, "Set2"),
col = brewer.pal(5, "Set3"),
fontface = "bold",
cat.col = brewer.pal(5, "Set3"),
cat.fontface = "bold"
)
2 保存为PDF
pdf("venn-pdf.pdf")
grid.draw(p)
dev.off()
3 保存为PNG
png("venn-png.png")
grid.draw(p)
dev.off()
更多参数:
col = NA # 边框设为无色
参考:https://www.r-graph-gallery.com/14-venn-diagramm.html
venn网页工具:http://bioinformatics.psb.ugent.be/webtools/Venn/
venn.diagram函数文档: https://www.rdocumentation.org/packages/VennDiagram/versions/1.6.20/topics/venn.diagram
The R Graph Gallery绘图教程:https://www.r-graph-gallery.com/index.html