韦恩图进阶!complexheatmap包画upset plot
Try to learn everything about something!
书接上回!!!!
前面介绍了多种韦恩图的画法,详细介绍了upsetR画upset plot的方法。
今天继续介绍upset plot,使用complexhatmap包。
构造数据
还是使用之前一直用的数据,新建一个7个元素的列表。
library(ComplexHeatmap)
## 载入需要的程辑包:grid
## ========================================
## ComplexHeatmap version 2.10.0
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
##
## If you use it in published research, please cite:
## Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional
## genomic data. Bioinformatics 2016.
##
## The new InteractiveComplexHeatmap package can directly export static
## complex heatmaps into an interactive Shiny app with zero effort. Have a try!
##
## This message can be suppressed by:
## suppressPackageStartupMessages(library(ComplexHeatmap))
## ========================================
sets <- list(
a = sample(1:500,300),
b = sample(1:500,350),
c = sample(1:500,420),
d = sample(1:500,300),
e = sample(1:500,400),
f = sample(1:500,380),
g = sample(1:500,400)
)
complexheatmap包提供了一个非常简单的函数可以把列表变为矩阵,同时也是画upsetplot图用到的格式:
m <- make_comb_mat(sets)
head(m)
## A combination matrix with 6 sets and 56 combinations.
## ranges of combination set size: c(1, 61).
## mode for the combination size: distinct.
## sets are on rows.
##
## Top 8 combination sets are:
## a b c d e f code size
## x x x x x x 111111 61
## x x x x x 111011 56
## x x x x x 011111 44
## x x x x x 101111 28
## x x x x 011011 23
## x x x x x 111110 22
## x x x x 101011 18
## x x x 001011 18
##
## Sets are:
## set size
## a 300
## b 350
## c 420
## d 300
## e 400
## f 380
基础画图
然后就可以直接画图了:
UpSet(m)
默认出来的图形不是很好看,可以通过几个参数进行调整。
m <- make_comb_mat(sets, top_n_sets = 5) # 为了方便选5个集合
ss = set_size(m) # 计算每个集合的大小
cs = comb_size(m) # 计算每个交集的大小
ss
## b c e f g
## 350 420 400 380 400
cs
## 11111 11110 11101 11011 10111 01111 11100 11010 11001 10110 10101 10011 01110
## 148 36 42 33 29 66 13 4 12 6 7 8 13
## 01101 01011 00111 11000 10100 10010 10001 01100 01010 01001 00110 00101 00011
## 14 13 7 2 4 2 3 7 7 9 3 4 5
## 10000 01000 00100
## 1 1 1
接下俩进行简单的调整:
UpSet(m, set_order = order(ss), # 集合顺序
comb_order = order(-cs), # 交集顺序
pt_size = unit(5,"mm"), lwd = 3, # 点的大小,线条粗细
comb_col = c("tomato","skyblue"), # 交集颜色
bg_col = c("#F0F0FF", "#FFF0F0"), # 背景色
bg_pt_col = "black" # 不在交集中的点的颜色
)
基本的绘图参数就这几个,不太够用,但是既然是complexheatmap,那肯定可以有超多自定义的细节。
upset as heatmap
可以通过把upset plot当过热图来看,基础的矩阵部分由*upset()*函数完成,剩余的条形图都通过画热图的方式搞定。
UpSet(m, set_order = order(ss), comb_order = order(-cs),
bg_pt_col = "grey80",
# 上方条形
top_annotation = upset_top_annotation(
m,height = unit(8,"cm"),
add_numbers = T,
gp = gpar(col = comb_degree(m))
),
# 左侧条形
left_annotation = upset_left_annotation(
m,
add_numbers = T,
gp = gpar(fill = "skyblue",col="skyblue")
)
)
是不是很强,如果你还不了解complexheatmap的强大用法,请翻看我之前的推文,关于这个包我一共写了8篇教程,方方面面都介绍到了!
movies数据集
下面还是使用之前用过的movies数据集进行演示,看看complexheatmap包画热图的强大之处。
movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"),
header = T, sep = ";")
str(movies)
## 'data.frame': 3883 obs. of 21 variables:
## $ Name : chr "Toy Story (1995)" "Jumanji (1995)" "Grumpier Old Men (1995)" "Waiting to Exhale (1995)" ...
## $ ReleaseDate: int 1995 1995 1995 1995 1995 1995 1995 1995 1995 1995 ...
## $ Action : int 0 0 0 0 0 1 0 0 1 1 ...
## $ Adventure : int 0 1 0 0 0 0 0 1 0 1 ...
## $ Children : int 1 1 0 0 0 0 0 1 0 0 ...
## $ Comedy : int 1 0 1 1 1 0 1 0 0 0 ...
## $ Crime : int 0 0 0 0 0 1 0 0 0 0 ...
## $ Documentary: int 0 0 0 0 0 0 0 0 0 0 ...
## $ Drama : int 0 0 0 1 0 0 0 0 0 0 ...
## $ Fantasy : int 0 1 0 0 0 0 0 0 0 0 ...
## $ Noir : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Horror : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Musical : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Mystery : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Romance : int 0 0 1 0 0 0 1 0 0 0 ...
## $ SciFi : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Thriller : int 0 0 0 0 0 1 0 0 0 1 ...
## $ War : int 0 0 0 0 0 0 0 0 0 0 ...
## $ Western : int 0 0 0 0 0 0 0 0 0 0 ...
## $ AvgRating : num 4.15 3.2 3.02 2.73 3.01 3.88 3.41 3.01 2.66 3.54 ...
## $ Watches : int 2077 701 478 170 296 940 458 68 102 888 ...
m <- make_comb_mat(movies, top_n_sets = 6)
画图,本质上还是画热图的各个部分:
genre <- c("Action", "Romance", "Horror", "Children", "SciFi", "Documentary")
m <- make_comb_mat(movies[, genre])
m <- m[comb_degree(m) > 0]
comb_elements <- lapply(comb_name(m), function(nm) extract_comb(m, nm))
years <- lapply(comb_elements, function(ind) movies$ReleaseDate[ind])
rating <- lapply(comb_elements, function(ind) movies$AvgRating[ind])
watches <- lapply(comb_elements, function(ind) movies$Watches[ind])
UpSet(t(m)) + rowAnnotation(years = anno_boxplot(years),
rating = anno_boxplot(rating),
watches = anno_boxplot(watches),
gap = unit(2, "mm"))
以上就是使用complexheatmap包画upset plot的用法,和upsetR各有千秋,个人感觉还是upsetR更加强大一点。
你们喜欢哪一个呢?
以上就是今天的内容,希望对你有帮助哦!欢迎点赞、在看、关注、转发!
欢迎在评论区留言或直接添加我的微信!
欢迎关注公众号:医学和生信笔记
“医学和生信笔记 公众号主要分享:1.医学小知识、肛肠科小知识;2.R语言和Python相关的数据分析、可视化、机器学习等;3.生物信息学学习资料和自己的学习笔记!
往期回顾
画一幅更好看的杠铃图!
你还不会画网络图?
R语言生信图表学习之网络图
可视化可爱的小蜜蜂
画一个好看的桑基图?