ComplexUpset是非常强的Veen图绘制软件包,平时Veen的绘制最多能达到6个相交,而此包对于就是针对6个以及以上的交互所准备的。特此推荐,以下模板可直接套用。
一、安装包
install.packages("ggplot2")
install.packages("ComplexUpset")
install.packages("ggplot2movies")
二、加载包
library(ggplot2)
library(ComplexUpset)
library(ggplot2movies)
三、加载数据
movies = as.data.frame(ggplot2movies::movies)
head(movies, 3)
genres = colnames(movies)[18:24]
genres
#将genres转换为布尔型
movies[genres] = movies[genres] == 1
t(head(movies[genres], 3))#倒置前三行进行查看
##创建子集以便快速运行
movies[movies$mpaa == '', 'mpaa'] = NA
movies = na.omit(movies)#删除缺失值
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, name='genre', width_ratio=0.1)
双图展示:
(
upset(movies, genres, name='genre', width_ratio=0.1, min_size=10, wrap=TRUE, set_sizes=FALSE)
+ ggtitle('Without empty groups (Short dropped)')
+ # adding plots is possible thanks to patchwork
upset(movies, genres, name='genre', width_ratio=0.1, min_size=10, keep_empty_groups=TRUE, wrap=TRUE, set_sizes=FALSE)
+ ggtitle('With empty groups')
)
展示所有交互:
set_size(8, 3)
upset(
movies, genres,
width_ratio=0.1,
min_size=10,
mode='inclusive_union',
base_annotations=list('Size'=(intersection_size(counts=FALSE, mode='inclusive_union'))),
intersections="all",
max_degree=3
)
添加组件
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, 3)
upset(movies,genres,
base_annotations=list(
'Intersection size'=intersection_size(
text=list(
vjust=-0.1,
hjust=-0.1,
angle=45),
text_colors=c(
on_background='brown', on_bar='yellow'))
+ annotate(
geom='text', x=Inf, y=Inf,
label=paste('Total:', nrow(movies)),
vjust=1, hjust=1
)
+ ylab('Intersection size')),
min_size=10,
width_ratio=0.1
)
给柱子添加颜色
set_size(8, 3)
upset(movies,genres,
base_annotations=list(
'Intersection size'=intersection_size(
text=list(
vjust=-0.1,
hjust=-0.1,
angle=45),
counts=T,
mapping=aes(fill=mpaa),
text_colors=c(
on_background='brown', on_bar='yellow'))
+ annotate(
geom='text', x=Inf, y=Inf,
label=paste('Total:', nrow(movies)),
vjust=1, hjust=1
)
+ ylab('Intersection size')),
min_size=10,
width_ratio=0.1
)
修改背景等
set_size(8, 3)
upset(movies,genres,
base_annotations=list(
'Intersection size'=intersection_size(
text=list(
vjust=-0.1,
hjust=-0.1,
angle=45),
counts=T,
mapping=aes(fill=mpaa),
text_colors=c(
on_background='brown', on_bar='yellow'))
+ annotate(
geom='text', x=Inf, y=Inf,
label=paste('Total:', nrow(movies)),
vjust=1, hjust=1
)
+theme(plot.background=element_rect(fill='#E5D3B3'))
+ ylab('Intersection size')),
set_sizes=(
upset_set_size()
+ theme(axis.text.x=element_text(angle=90))),
min_size=10,
width_ratio=0.1
)
下部添加颜色
set_size(8, 3)
upset(movies,genres,
base_annotations=list(
'Intersection size'=intersection_size(
text=list(
vjust=-0.1,
hjust=-0.1,
angle=45),
counts=T,
mapping=aes(fill=mpaa),
text_colors=c(
on_background='brown', on_bar='yellow'))
+ annotate(
geom='text', x=Inf, y=Inf,
label=paste('Total:', nrow(movies)),
vjust=1, hjust=1
)
+theme(plot.background=element_rect(fill='#E5D3B3'))),
set_sizes=(
upset_set_size()
+ theme(axis.text.x=element_text(angle=90))),
min_size=10,
stripes=c('cornsilk1', 'deepskyblue1'),
width_ratio=0.1
)
交互的高亮显示
set_size(8, 3)
upset(
movies, c("Action", "Comedy", "Drama"),
width_ratio=0.2,
group_by='sets',
queries=list(
upset_query(
intersect=c('Drama', 'Comedy'),
color='red',
fill='red',
only_components=c('intersections_matrix', 'Intersection size')
),
upset_query(group='Drama', color='blue'),
upset_query(group='Comedy', color='orange'),
upset_query(group='Action', color='purple'),
upset_query(set='Drama', fill='blue'),
upset_query(set='Comedy', fill='orange'),
upset_query(set='Action', fill='purple')
)
)
欢迎关注微信公众号“生信小书生”,学习科研绘图技巧。