大家好,又见面了,我是你们的朋友全栈君。
最近对利用R绘制venn图进行了一些学习。参考了网上很多资料:
- 【R作图】在R中绘制韦恩图的几种方法 和 一些漂亮的venn图
- 如何使用R来绘制韦恩图(Venn Diagram)
- venn.diagram: Make a Venn Diagram
- how to add legends and values in a Venn diagram using R (VennEuler package)?
另外再推荐一个在线绘制venn图的网站(除了广告较多都挺好的):https://www.meta-chart.com/venn
具体包括下面三个包: limma、venneuler、VennDiagram。总的来说,三个包都有着各自的不足。下面会一一进行说明,这里先放上结论:综合方便程度以及函数的多样性而言,VennDiagram > venneuler > limma
。
limma
首先针对limma包,输入下两行就可以直接进行安装:
source("http://www.bioconductor.org/biocLite.R")
biocLite("limma")
虽然只用输入两行,但是运行之后你就会发现,依赖包需要安装到地老天荒,海枯石烂,花儿都谢了。(而且还可能安装失败)
(我们这里假设安装成功
之后利用vennDiagram()
按照指定的用法就可进行venn图的绘制了,但这个包有一个很大的局限,就是最多只能够绘制3个集合,一旦超过就无法继续绘制了,而且绘制的效果也比较丑,如下所示。(由于实在没有安装成功,这里参考了网址:venn: Venn Diagrams)
实在不符合审美,我们进入下一个包。
venneuler
接着是venneuler
,这个包绘制出来的图案就比上一个包要漂亮了许多,如下图所示:
vd <- venneuler(c(A=0.3, B=0.3, C=1.1, "A&C"=0.2, "B&C"=0.1 ,"A&B&C"=0.08))
plot(vd)
这个包也是存在缺点的,首先可能由于自己的水平没有到家,并不知道如何在集合上面显示数字。
为了克服第一个问题,在集合上面显示数字,我们可以用下面这个包——eulerr
。
eulerr
这个包可以显示数字或者比例(quantities
),并且可以自定义边框(edges
),颜色(fills
)等等。但还是没解决一个问题,如下图所示,A与B应该是没有交集,不应该重合在一起,但用代码绘制后使其有了交集
library(eulerr)
vd <- euler(c(A = 0.3, B = 0.3, C = 1.1,
"A&B" = 0, "A&C" = 0.2, "B&C" = 0.1,
"A&B&C" = 0.1))
plot(vd,
fills = list(fill = c("#fbb4ae", "#b3cde3", "#ccebc5"), alpha = 0.6),
labels = list(col = "white", font = 4),
edges = FALSE,
quantities = TRUE)
VennDiagram
最后是VennDiagram
,这个包功能非常完善,没有交集就不会重合,网上关于其的博客也是最多的。其绘制出来的图也可以非常美观
虽然这个包功能相对完善,但也有着其缺点,就是每个圈不会随着集合中数量的增大而增大,反映不出每个集合的相对大小。不过即使有这样的缺点,现在能搜索到的资源当中,比较丰富的就是这个包了。
下面的这些图都是VennDiagram
绘制出来的~
搬运工
由于说明文档:venn.diagram: Make a Venn Diagram 中只有代码,没有每段代码输出的结果与其直接对应。这里我做一只勤劳的小蜜蜂,把运行的结果全部贴上来,便于之后查找以及DIY。
先放上汇总效果图:
具体实现
首先载入函数包并设置路径,然后全部运行就可以将所有图片全部进行输出了。(这里不将所有的图贴上来,因为贴上来的图总是图裂显示不出来,所以只是将代码copy过来,具体可以自己运行参照着语句进行对比)
setwd('D:\\Pictures\\Blog\\venn')
library(VennDiagram)
# Note: most examples are listed as dontrun to meet CRAN requirements,
# but all should work as-is!
# compact and minimal notation
## Not run:
venn.plot <- venn.diagram(
list(A = 1:150, B = 121:170),
"Venn_2set_simple.jpeg"
);
venn.plot <- venn.diagram(
list(A = 1:150, B = 121:170, C = 101:200),
"Venn_3set_simple.jpeg"
);
## End(Not run)
# a more elaborate two-set Venn diagram with title and subtitle
venn.plot <- venn.diagram(
x = list(
"A" = 1:100,
"B" = 96:140
),
filename = "Venn_2set_complex.jpeg",
scaled = TRUE,
ext.text = TRUE,
ext.line.lwd = 2,
ext.dist = -0.15,
ext.length = 0.9,
ext.pos = -4,
inverted = TRUE,
cex = 2.5,
cat.cex = 2.5,
rotation.degree = 45,
main = "Complex Venn Diagram",
sub = "Featuring: rotation and external lines",
main.cex = 2,
sub.cex = 1
);
## Not run:
# sample three-set Euler diagram
venn.plot <- venn.diagram(
x = list(
"Num A" = paste("Num", 1:100),
"Num B" = c(paste("Num", 61:70), paste("Num", 71:100)),
"Num C" = c(paste("Num", 41:60), paste("Num", 61:70))),
euler.d = TRUE,
filename = "Euler_3set_simple.jpeg",
cat.pos = c(-20, 0, 20),
cat.dist = c(0.05, 0.05, 0.02),
cex = 2.5,
cat.cex = 2.5,
reverse = TRUE
);
# sample three-set Euler diagram
venn.plot <- venn.diagram(
x = list(
A = c(1:10),
B = c(11:90),
C = c(81:90)
),
euler.d = TRUE,
filename = "Euler_3set_scaled.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.pos = 0
);
## End(Not run)
# sample four-set Venn Diagram
A <- sample(1:1000, 400, replace = FALSE);
B <- sample(1:1000, 600, replace = FALSE);
C <- sample(1:1000, 350, replace = FALSE);
D <- sample(1:1000, 550, replace = FALSE);
E <- sample(1:1000, 375, replace = FALSE);
venn.plot <- venn.diagram(
x = list(
A = A,
D = D,
B = B,
C = C
),
filename = "Venn_4set_pretty.jpeg",
col = "transparent",
fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
alpha = 0.50,
label.col = c("orange", "white", "darkorchid4", "white",
"white", "white", "white", "white", "darkblue", "white",
"white", "white", "white", "darkgreen", "white"),
cex = 1.5,
fontfamily = "serif",
fontface = "bold",
cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"),
cat.cex = 1.5,
cat.pos = 0,
cat.dist = 0.07,
cat.fontfamily = "serif",
rotation.degree = 270,
margin = 0.2
);
# sample five-set Venn Diagram
venn.plot <- venn.diagram(
x = list(
A = A,
B = B,
C = C,
D = D,
E = E
),
filename = "Venn_5set_pretty.jpeg",
col = "black",
fill = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"),
alpha = 0.50,
cex = c(1.5, 1.5, 1.5, 1.5, 1.5, 1, 0.8, 1, 0.8, 1, 0.8, 1, 0.8,
1, 0.8, 1, 0.55, 1, 0.55, 1, 0.55, 1, 0.55, 1, 0.55, 1, 1, 1, 1, 1, 1.5),
cat.col = c("dodgerblue", "goldenrod1", "darkorange1", "seagreen3", "orchid3"),
cat.cex = 1.5,
cat.fontface = "bold",
margin = 0.05
);
# Complex three-way Venn with labels & sub-/super-scripts
venn.plot <- venn.diagram(
x = list(
I = c(1:60, 61:105, 106:140, 141:160, 166:175, 176:180, 181:205,
206:220),
II = c(531:605, 476:530, 336:375, 376:405, 181:205, 206:220, 166:175,
176:180),
III = c(61:105, 106:140, 181:205, 206:220, 221:285, 286:335, 336:375,
376:405)
),
category.names = c(
expression( bold('A'['1: subscript']) ),
expression( bold('B'^'2: going up') ),
expression( paste(bold('C'^'3'), bold('X'['i' <= 'r'^'2']^'2') ) )
),
filename = 'Fig3-1_triple_labels_sub_and_superscripts.jpeg',
output = TRUE,
height = 3000,
width = 3000,
resolution = 300,
compression = 'lzw',
units = 'px',
lwd = 6,
lty = 'blank',
fill = c('yellow', 'purple', 'green'),
cex = 3.5,
fontface = "bold",
fontfamily = "sans",
cat.cex = 3,
cat.fontface = "bold",
cat.default.pos = "outer",
cat.pos = c(-27, 27, 135),
cat.dist = c(0.055, 0.055, 0.085),
cat.fontfamily = "sans",
rotation = 1
);
# Complex 3-way Venn using expressions
venn.plot <- venn.diagram(
x = list(
"Num A" = paste("Num", 1:100),
"Num B" = c(paste("Num", 61:70), paste("Num", 71:100)),
"Num C" = c(paste("Num", 41:60), paste("Num", 61:70))),
category.names = c(
expression( bold('A'['1']) ),
expression( bold('A'['2']) ),
expression( bold('A'['3']) )
),
euler.d = TRUE,
filename = "Fig3-2_Euler_3set_simple_with_subscripts.jpeg",
cat.pos = c(-20, 0, 20),
cat.dist = c(0.05, 0.05, 0.02),
cex = 2.5,
cat.cex = 2.5,
reverse = TRUE
);
## Not run:
# Example to print to screen
venn.plot <- venn.diagram(
x = list(
sample1 = c(1:40),
sample2 = c(30:60)
),
filename = NULL
);
# Save picture to non-jpeg file type
# currently working on adding this functionality directly into venn.diagram
venn.plot <- venn.diagram(
x = list (
A = 1:10,
B = 6:25
),
filename = NULL
);
jpeg("venn_jpeg.jpg");
grid.draw(venn.plot);
dev.off();
## End(Not run)
#dontrun-starts-here
### NB: All figures from the paper can be run, but are turned off from
### automatic execution to reduce burden on CRAN computing resources.
## Not run:
# Figure 1A
venn.plot <- venn.diagram(
x = list(
Label = 1:100
),
filename = "1A-single_Venn.jpeg",
col = "black",
lwd = 9,
fontface = "bold",
fill = "grey",
alpha = 0.75,
cex = 4,
cat.cex = 3,
cat.fontface = "bold",
);
# Figure 1B
venn.plot <- venn.diagram(
x = list(
X = 1:150,
Y = 121:180
),
filename = "1B-double_Venn.jpeg",
lwd = 4,
fill = c("cornflowerblue", "darkorchid1"),
alpha = 0.75,
label.col = "white",
cex = 4,
fontfamily = "serif",
fontface = "bold",
cat.col = c("cornflowerblue", "darkorchid1"),
cat.cex = 3,
cat.fontfamily = "serif",
cat.fontface = "bold",
cat.dist = c(0.03, 0.03),
cat.pos = c(-20, 14)
);
# Figure 1C
venn.plot <- venn.diagram(
x = list(
R = c(1:70, 71:110, 111:120, 121:140),
B = c(141:200, 71:110, 111:120, 201:230),
G = c(231:280, 111:120, 121:140, 201:230)
),
filename = "1C-triple_Venn.jpeg",
col = "transparent",
fill = c("red", "blue", "green"),
alpha = 0.5,
label.col = c("darkred", "white", "darkblue", "white",
"white", "white", "darkgreen"),
cex = 2.5,
fontfamily = "serif",
fontface = "bold",
cat.default.pos = "text",
cat.col = c("darkred", "darkblue", "darkgreen"),
cat.cex = 2.5,
cat.fontfamily = "serif",
cat.dist = c(0.06, 0.06, 0.03),
cat.pos = 0
);
# Figure 1D
venn.plot <- venn.diagram(
x = list(
I = c(1:60, 61:105, 106:140, 141:160, 166:175, 176:180, 181:205,
206:220),
IV = c(531:605, 476:530, 336:375, 376:405, 181:205, 206:220, 166:175,
176:180),
II = c(61:105, 106:140, 181:205, 206:220, 221:285, 286:335, 336:375,
376:405),
III = c(406:475, 286:335, 106:140, 141:160, 166:175, 181:205, 336:375,
476:530)
),
filename = "1D-quadruple_Venn.jpeg",
col = "black",
lty = "dotted",
lwd = 4,
fill = c("cornflowerblue", "green", "yellow", "darkorchid1"),
alpha = 0.50,
label.col = c("orange", "white", "darkorchid4", "white", "white", "white",
"white", "white", "darkblue", "white",
"white", "white", "white", "darkgreen", "white"),
cex = 2.5,
fontfamily = "serif",
fontface = "bold",
cat.col = c("darkblue", "darkgreen", "orange", "darkorchid4"),
cat.cex = 2.5,
cat.fontfamily = "serif"
);
# Figure 2-1
venn.plot <- venn.diagram(
x = list(
A = 1:105,
B = 101:115
),
filename = "2-1_special_case_ext-text.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.pos = c(-20, 20),
ext.line.lty = "dotted",
ext.line.lwd = 2,
ext.pos = 12,
ext.dist = -0.12,
ext.length = 0.85
);
# Figure 2-2
venn.plot <- venn.diagram(
x = list(
A = 1:100,
B = 1:10
),
filename = "2-2_special_case_pairwise-inclusion.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.pos = 0
);
# Figure 2-3
venn.plot <- venn.diagram(
x = list(
A = 1:150,
B = 151:250
),
filename = "2-3_special_case_pairwise-exclusion.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.pos = c(0, 0),
cat.dist = 0.05
);
# Figure 2-4
venn.plot <- venn.diagram(
x = list(
A = c(1:50, 101:140, 141:160, 161:170),
B = c(171:230, 101:140, 161:170, 291:320),
C = c(141:160, 161:170, 291:320)
),
filename = "2-4_triple_special_case-001.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.dist = c(0.05, 0.05, -0.1)
);
# Figure 2-5
venn.plot <- venn.diagram(
x = list(
A = c(1:100),
B = c(61:70, 71:100),
C = c(41:60, 61:70)
),
filename = "2-5_triple_special_case-012AA.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.pos = c(-25, 0, 30),
cat.dist = c(0.05, 0.05, 0.02)
);
# Figure 2-6
venn.plot <- venn.diagram(
x = list(
A = c(1:90),
B = c(1:25),
C = c(1:5)
),
filename = "2-6_triple_special_case-022AAAO.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.pos = 0,
cat.dist = c(0.03, 0.03, 0.01)
);
# Figure 2-7
venn.plot <- venn.diagram(
x = list(
A = c(1:20),
B = c(21:80),
C = c(81:210)
),
filename = "2-7_triple_special_case-100.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.dist = 0.05
);
# Figure 2-8
venn.plot <- venn.diagram(
x = list(
A = c(1:80),
B = c(41:150),
C = c(71:100)
),
filename = "2-8_triple_special_case-011A.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.dist = c(0.07, 0.07, 0.02),
cat.pos = c(-20, 20, 20)
);
# Figure 2-9
venn.plot <- venn.diagram(
x = list(
A = c(1:10),
B = c(11:90),
C = c(81:90)
),
filename = "2-9_triple_special_case-121AO.jpeg",
cex = 2.5,
cat.cex = 2.5,
cat.pos = 0,
cat.dist = c(0.04, 0.04, 0.02),
reverse = TRUE
);
#dontrun-ends-here
## End(Not run)
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/162694.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...