R語言繪圖包系列:
- R語言繪圖包01--優(yōu)秀的拼圖包patchwork
- R語言繪圖包02--熱圖pheatmap
- R語言繪圖包03--火山圖EnhancedVolcano
- R語言繪圖包04--GOplot:富集分析結(jié)果可視化
- R語言繪圖包05--韋恩圖的繪制:ggvenn和VennDiagram
- R語言繪圖包06--基因表達(dá)相關(guān)性繪圖corrplot
一般在對數(shù)據(jù)取交集的時候,通常使用韋恩圖。但韋恩圖的可視范圍有限,對于超過五個以上的數(shù)據(jù)集取交集會顯得很凌亂。這時候就可以使用UpSetR包。
UpsetR接受三種類型的數(shù)據(jù)輸入:
表格,也是行是元素,列是數(shù)據(jù)集分配和額外信息的數(shù)據(jù)框。
元素名的集合(fromList)
venneuler包引入的用于描述集合交集的向量fromExpression。
1. 基礎(chǔ)用法
install.packages(UpSetR)
library(UpSetR)
require(ggplot2); require(plyr); require(gridExtra); require(grid);
movies <- read.csv( system.file("extdata", "movies.csv", package = "UpSetR"), header=TRUE, sep=";" )
head(movies)
# Name ReleaseDate Action Adventure Children Comedy Crime Documentary
# 1 Toy Story (1995) 1995 0 0 1 1 0 # 0
# 2 Jumanji (1995) 1995 0 1 1 0 0 0
# 3 Grumpier Old Men (1995) 1995 0 0 0 1 0 0
# 4 Waiting to Exhale (1995) 1995 0 0 0 1 0 0
# 5 Father of the Bride Part II (1995) 1995 0 0 0 1 0 0
# 6 Heat (1995) 1995 1 0 0 0 1 0
# Drama Fantasy Noir Horror Musical Mystery Romance SciFi Thriller War Western AvgRating Watches
# 1 0 0 0 0 0 0 0 0 0 0 0 4.15 2077
# 2 0 1 0 0 0 0 0 0 0 0 0 3.20 701
# 3 0 0 0 0 0 0 1 0 0 0 0 3.02 478
# 4 1 0 0 0 0 0 0 0 0 0 0 2.73 170
# 5 0 0 0 0 0 0 0 0 0 0 0 3.01 296
# 6 0 0 0 0 0 0 0 0 1 0 0 3.88 940
upset(movies, nsets = 7, nintersects = 30, mb.ratio = c(0.5, 0.5),
order.by = c("freq", "degree"), decreasing = c(TRUE,FALSE))

這個圖由三個子圖組成:1. 表示交集大小的柱狀圖(上方);2. 表示集合大小的條形圖(下左);3. 表示集合之間的交疊矩陣(下右),矩陣的列表示每種交集組合,對應(yīng)于柱狀圖的橫坐標(biāo);矩陣的行表示集合,對應(yīng)于條形圖的縱坐標(biāo)
主要參數(shù):
nsets: 最多展示多少個集合數(shù)據(jù)(上圖有多少行)。畢竟原來有20多種電影類型,放不完的
nintersects: 展示多少交集(上圖有多少列)。
mb.ratio: 點點圖和條形圖的比例。
order.by: 交集如何排序。這里先根據(jù)freq,然后根據(jù)degree
decreasing: 變量如何排序。這里表示freq降序,degree升序
2. 精細(xì)化繪圖
我們還能在圖中描述出1970-1980年恐怖片和動作片的情況
between <- function(row, min, max){
newData <- (row["ReleaseDate"] < max) & (row["ReleaseDate"] > min)
}
upset(movies, sets = c("Drama", "Comedy", "Action", "Thriller", "Western", "Documentary"),
queries = list(list(query = intersects, params = list("Drama", "Action")),
list(query = between, params = list(1970, 1980), color = "red", active = TRUE)))

通過attribute.plots參數(shù)添加屬性圖
upset(movies,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))
# attribute.plots接受各個plot函數(shù)組成的作圖函數(shù),可以用自帶的,也可以自己寫,只要保證里面的參數(shù)設(shè)置正確了。

upset(movies, attribute.plots = attributeplots,
queries = list(list(query = between, params = list(1920, 1940)),
list(query = intersects, params = list("Drama"), color= "red"),
list(query = elements, params = list("ReleaseDate", 1990, 1991, 1992))),
main.bar.color = "yellow")

參考:http://www.itdecent.cn/p/324aae3d5ea4
https://mp.weixin.qq.com/s/DSyaje-nFb8o--kuzmTvaA