关于用R绘制韦恩图的方法之前有文章也介绍了一部分,在文章《R 多数据集可视化工具-UpSetR》中介绍了 UpSetR 功能特点,虽然可以绘制超过3个组别的韦恩图展示,但是目前已经没有大的更新。
ComplexUpset结合了Upset和ggplot2等R包的特点可以完成更加复杂的多组数据比较的可视化展示。
1)安装
# 最新开发版
if(!require(devtools)) install.packages("devtools")
devtools::install_github("krassowski/complex-upset")
# 正式版
install.packages('ComplexUpset')
2)示例
library(ggplot2)
library(ComplexUpset)
# 需要安装ggplot2movies
# install.packages('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)
set_size = function(w, h, factor=1.5) {
s = 1 * factor
options(
repr.plot.width=w * s,
repr.plot.height=h * s,
repr.plot.res=100 / factor,
jupyter.plot_mimetypes='image/png',
jupyter.plot_scale=1
)
}
set_size(8, 3)
upset(
movies,
genres,
base_annotations=list(
'Intersection size'=intersection_size(
counts=FALSE,
mapping=aes(fill=mpaa)
)
),
width_ratio=0.1
)

set_size(8, 5)
upset(
movies,
genres,
annotations = list(
'MPAA Rating'=(
ggplot(mapping=aes(fill=mpaa))
+ geom_bar(stat='count', position='fill')
+ scale_y_continuous(labels=scales::percent_format())
+ scale_fill_manual(values=c(
'R'='#E41A1C', 'PG'='#377EB8',
'PG-13'='#4DAF4A', 'NC-17'='#FF7F00'
))
+ ylab('MPAA Rating')
)
),
width_ratio=0.1
)

set_size(8, 8)
# keep the same jitter for identical plots
set.seed(0)
# 需要安装ggbeeswarm
# install.packages('ggbeeswarm')
upset(
movies,
genres,
# 理论上可以组合多个图层
annotations = list(
# 1st method - passing list:
'Length'=list(
aes=aes(x=intersection, y=length),
# provide a list if you wish to add several geoms
geom=geom_boxplot(na.rm=TRUE)
),
# 2nd method - using ggplot
'Rating'=(
# note that aes(x=intersection) is supplied by default and can be skipped
ggplot(mapping=aes(y=rating))
# checkout ggbeeswarm::geom_quasirandom for better results!
+ geom_jitter(aes(color=log10(votes)), na.rm=TRUE)
+ geom_violin(alpha=0.5, na.rm=TRUE)
),
# 3rd method - using `upset_annotate` shorthand
'Budget'=upset_annotate('budget', geom_boxplot(na.rm=TRUE))
),
min_size=10,
width_ratio=0.1
)

更多示例可以参考:官方示例
参考资料:
1.https://github.com/krassowski/complex-upset
2.https://krassowski.github.io/complex-upset/articles/Examples_R.html