【绘图】使用Upset图替代韦恩图

Last updated on September 27, 2025 pm

配置环境

1
2
conda create -n complexupset conda-forge::r-tidyverse conda-forge::r-irkernel conda-forge::r-complexupset -y
conda run -n complexupset Rscript -e "IRkernel::installspec(name='complexupset', displayname='r-complexupset')"
1
install.packages('ggplot2movies') # 测试用的数据,可以不安装

使用示例

1
2
3
4
5
require(ComplexUpset)
require(tidyverse)
require(RColorBrewer)
options(repr.plot.width=6, repr.plot.height=12)
RColorBrewer::display.brewer.all(type = "all")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require(ggplot2movies)
movies = as.data.frame(ggplot2movies::movies)
genres <- colnames(movies)[18:24]
movies[genres] = (movies[genres] == 1)
# 仅用子集进行演示
movies[movies$mpaa == '', 'mpaa'] = NA
movies = na.omit(movies)#删除缺失值
t(head(movies[genres], 3))#倒置前三行进行查看

data <- movies[genres]
data %>% head
# 多维二分类变量数据,值为布尔类型,colnames为变量名
# Action Animation Comedy Drama Documentary Romance Short
# 1 FALSE FALSE TRUE FALSE FALSE TRUE FALSE
# 2 FALSE FALSE TRUE FALSE FALSE TRUE FALSE
# 3 FALSE FALSE TRUE FALSE FALSE FALSE FALSE
# 4 FALSE FALSE TRUE TRUE FALSE TRUE FALSE
# 5 TRUE FALSE FALSE FALSE FALSE FALSE FALSE
# 6 FALSE FALSE FALSE TRUE FALSE FALSE FALSE
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
29
options(repr.plot.width=6, repr.plot.height=6)
upset(
data,
c("Action", "Comedy", "Drama", 'Romance'),
name='genre',
width_ratio=0.1,
height_ratio=0.382,
min_size=10,
set_sizes=FALSE,
base_annotations=list(
"Intersection size" = intersection_size(
text=list(
hjust = -0.1,
angle = 45
),
mapping = aes(fill = "bars_color")
) + scale_fill_manual(
values = c("bars_color" = RColorBrewer::brewer.pal(3, 'Set1')[1]),
guide = "none"
) + annotate(
geom = 'text',
x=Inf, y=Inf,
label = paste('Total:', nrow(data)),
vjust=1, hjust=1
) + theme(panel.grid=element_blank())
),
sort_intersections_by = c("cardinality", "degree"),
stripes=RColorBrewer::brewer.pal(9, 'Set1')
)


【绘图】使用Upset图替代韦恩图
https://hexo.limour.top/replace-venn-diagram-with-upset-diagram
Author
Limour
Posted on
September 27, 2025
Updated on
September 27, 2025
Licensed under