韦恩图

在R语言中绘制韦恩图通常有两种方法,小于5个集合的用R包VennDiagram即可完成绘制;超过5个的使用R包UpSetR进行绘制。

VennDiagram

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
rm(list = ls())

data.test = data.frame(A = rep(c('a','b','c'),c(10,23,17)),
B = rep(c('b','c','e'),c(15,25,10)),
C = rep(c('a','c','e'),c(18,12,20)),
D = rep(c('a','c','d'),c(18,12,20)))

library(VennDiagram)
# 计算重叠

venn.diagram(x = list(A = data.test$A,
D = data.test$D,
B = data.test$B,
C = data.test$C),
height = 3000, # 高度
width = 3000, # 宽度
resolution = 500, # 分辨率
main = 'test', # 标题
main.cex = 4, # 标题字体大小
main.col = 'red', # 标题颜色
fill = c('red','blue','green','orange'), # 填充色
col = 'black',# 边框颜色
lwd = 2, # 线宽度
alpha = .5, # 透明度
cat.cex = 2, # 标签字体大小
filename = 'figures/venn.test.tiff'# 文件名
)

1

当集合很多的时候怎么办?下面这个图是2012年Nature上的图$^{[1]}$,看起来真的是好复杂啊!这时候就该UpSet上场了。

5

UpSetR

0

UpSetR有对应的网站:http://caleydo.org/tools/upset/。还有Shiny APP:https://gehlenborglab.shinyapps.io/upsetr/。R包的使用可以参考对应的GitHub:https://github.com/hms-dbmi/UpSetR。也有开发者使用Python开发了对应的Python应用:https://github.com/ImSoErgodic/py-upset。

其中,Shiny APP的示例视频是YouTube上的,我把它下载上传到腾讯视频了,点击观看即可。

YouTube观看地址:https://www.youtube.com/watch?v=-IfF2wGw7Qk&t=188s

腾讯视频观看地址:https://v.qq.com/x/page/l09525p5h4q.html

1
2
3
4
5
6
7
8
9
10
11
library(UpSetR)
# 直接读取R包示例数据
movies <- read.csv( system.file("extdata", "movies.csv",
package = "UpSetR"), header=T, sep=";" )
mutations <- read.csv( system.file("extdata", "mutations.csv",
package = "UpSetR"), header=T, sep = ",")

upset(mutations,
sets = c("PTEN", "TP53", "EGFR", "PIK3R1", "RB1"),
sets.bar.color = "#56B4E9",
order.by = "freq")

下面这段代码可以展示更多内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
upset(movies, # 绘图数据集
sets.bar.color = "red", # 侧边柱子颜色
matrix.color = 'blue', # 相互关系连线(点)的颜色
main.bar.color = 'black', # 交互数量柱状图颜色及附图颜色
mainbar.y.label = 'Intersection Size', # 主柱状图Y轴标题
sets.x.label = '这是X轴标题', # 左侧柱状图X轴标题
point.size = 1.8, # 交互点的大小
line.size = 1.2, # 交互线的宽度
att.pos = 'bottom',
att.color = 'yellowgreen', # 变量展示图颜色
number.angles = 0, # 柱状图上数字旋转角度
group.by = 'degree', # 数据分组标准可以降序(degree)也可以按数据集(sets)
# 下面的代码主要是展示感兴趣的变量之间的关系
attribute.plots=list(gridrows=60,
plots=list(list(plot=scatter_plot,
x="ReleaseDate",
y="AvgRating"),
list(plot=scatter_plot,
x="ReleaseDate",
y="Watches"),
list(plot=scatter_plot,
x="Watches",
y="AvgRating"),
list(plot=histogram,
x="ReleaseDate")),
ncols = 2))

3

我觉得最酷炫的地方在于可以自定义下方附图函数,展示想要的任何图像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 构建绘图函数
plot.test = function(mydata,x,y){
library(ggplot2)
my.plot = (ggplot(data = mydata, aes(x = as.character(mydata[,x]),
y = mydata[,y],
fill = as.character(mydata[,x])))+
geom_boxplot()+
theme_classic()+
labs(x = x, y = y)+
theme(legend.position = 'none'))
}

# 使用绘图函数
upset(movies,
sets = c("Action", "Adventure", "Children", "War", "Noir"),
attribute.plots=list(gridrows = 100,
ncols = 1,
plots = list(list(plot=plot.test, # 使用函数
x="ReleaseDate",
y="Watches",
queries=T))))

4

参考文献

[1] D’hont, Angélique, et al. “The banana (Musa acuminata) genome and the evolution of monocotyledonous plants.” Nature 488.7410 (2012): 213-217.


💌lixiang117423@foxmail.com
💌lixiang117423@gmail.com


韦恩图
https://lixiang117423.github.io/article/plot4venn/
作者
小蓝哥
发布于
2022年1月20日
许可协议