作者:嚴(yán)濤
簡(jiǎn)介
集合可視化我們用得最多的是韋恩圖,韋恩圖在集合數(shù)少的時(shí)候是很好用的,但是當(dāng)集合數(shù)多比如五個(gè)以上的時(shí)候那就會(huì)看花眼了,比如下面這副含有6個(gè)集合韋恩圖,是發(fā)表在Nature上的文章里的,漂亮是漂亮,但是眼都快看花了。
今天介紹一個(gè)R包UpSetR,專門用來集合可視化,來源于UpSet,Python里面也有一個(gè)相似的包py-upset。此外還有個(gè)UpSetR shiny app以及源代碼.
安裝
兩種方式安裝:
#從CRAN安裝install.packages("UpSetR")#從Github上安裝devtools::install_github("hms-dbmi/UpSetR")
數(shù)據(jù)導(dǎo)入
UpSetR提供了兩個(gè)函數(shù)fromList以及fromExpression將數(shù)據(jù)轉(zhuǎn)換為UpsetR適用的數(shù)據(jù)格式。
#fromListlistinput <- list(one = c(1, 2, 3, 5, 7, 8, 11, 12, 13), two = c(1, 2, 4, 5,10), three = c(1, 5, 6, 7, 8, 9, 10, 12, 13))#fromExpressionexpressionInput <- c(one = 2, two = 1, three = 2, `one&two` = 1, `one&three` = 4,`two&three` = 1, `one&two&three` = 2)
接下來就可以繪制繪制圖形了
library(UpSetR)upset(fromList(listinput), order.by = "freq")#下面繪制的圖形等同于上圖upset(fromExpression(expressionInput), order.by = "freq")
參數(shù)詳解
下面所有的例子都將使用UpSetR內(nèi)置的數(shù)據(jù)集movies來繪制。
#導(dǎo)入數(shù)據(jù)movies <- read.csv(system.file("extdata", "movies.csv", package = "UpSetR"), header = TRUE, sep = ";")#先大致瀏覽一下該數(shù)據(jù)集,數(shù)據(jù)集太長(zhǎng),就只看前幾列knitr::kable(head(movies[,1:10]))
該數(shù)據(jù)集展示的是電影名(name)、發(fā)行時(shí)間(ReleaseDate)以及電影類型,多了去了就不詳講了,自個(gè)可以看去。
UpsetR繪制集合可視化圖形使用函數(shù)upset()。
upset(movies, nsets = 6, number.angles = 30, point.size = 2, line.size = 1, mainbar.y.label = "Genre Intersections", sets.x.label = "Movies Per Genre", text.scale = c(1.3, 1.3, 1, 1, 1.5, 1))
解釋一下上面部分參數(shù):
nsets: 顧名思義,就是展示幾個(gè)集合,movies數(shù)據(jù)集由20幾個(gè)集合,不可能全部展示,另外從圖中可以看出,這6個(gè)集合應(yīng)該不是按順序選擇的。numble.angle: 柱子上的數(shù)字看到了吧,這個(gè)參數(shù)就是調(diào)整數(shù)字角度的,可有可無的mainbar.y.label/sets.x.label:坐標(biāo)軸名稱text.scale(): 有六個(gè)數(shù)字,分別控制c(intersection size title, intersection size tick labels, set size title, set size tick labels, set names, numbers above bars)。很多時(shí)候我們想要看特定的幾個(gè)集合,UpSetR滿足我們的需求。
upset(movies, sets = c("Action", "Adventure", "Comedy", "Drama", "Mystery","Thriller", "Romance", "War", "Western"), mb.ratio = c(0.55, 0.45), order.by = "freq")
文中的參數(shù):
mb.ratio: 控制上方條形圖以及下方點(diǎn)圖的比例order.by: 如何排序,這里freq表示從大到小排序展示,其他選項(xiàng)有degree以及先按freq再按degree排序。各個(gè)變量也可以通過參數(shù)keep.order來排序
upset(movies, sets = c("Action", "Adventure", "Comedy", "Drama", "Mystery","Thriller", "Romance", "War", "Western"), mb.ratio = c(0.55, 0.45), order.by = "freq",keep.order = TRUE)
也可以按group進(jìn)行展示,這圖展示的就是按各個(gè)變量自身、兩個(gè)交集、三個(gè)交集…依次展示。參數(shù)cutoff控制每個(gè)group顯示幾個(gè)交集。
參數(shù)intersects控制總共顯示幾個(gè)交集。
upset(movies, nintersects = 70, group.by = "sets", cutoff = 7)
還有很多參數(shù)比如控制顏色的參數(shù),點(diǎn)、線大小等,具體可查看?upset
queries參數(shù)
queries參數(shù)分為四個(gè)部分:query,?param,?color,?active.
query: 指定哪個(gè)query,UpSetR有內(nèi)置的,也可以自定義,說到底就是一個(gè)查詢函數(shù)param: list, query作用于哪個(gè)交集color:每個(gè)query都是一個(gè)list,里面可以設(shè)置顏色,沒設(shè)置的話將調(diào)用包里默認(rèn)的調(diào)色板active:被指定的條形圖是否需要顏色覆蓋,TRUE的話顯示顏色,F(xiàn)ALSE的話則在條形圖頂端顯示三角形內(nèi)置的intersects query
upset(movies, queries = list(list(query=intersects, params=list("Drama", "Comedy", "Action"), color="orange", active=T),list(query=intersects, params=list("Drama"), color="red", active=F),list(query=intersects, params=list("Action", "Drama"), active=T)))
內(nèi)置的elements?query
此query可以可視化特定交集在不同條件下的情況
upset(movies, queries = list(list(query=elements, params=list("AvgRating", 3.5, 4.1), color="blue", active=T),list(query=elements, params=list("ReleaseDate", 1980, 1990, 2000), color="red", active=F)))
自定義一個(gè)query
myfunc <- function(row, release, rating){newdata <- (row["ReleaseDate"]%in%release)&(row["AvgRating"]>rating)}upset(movies, queries = list(list(query=myfunc, params=list(c(1950,1960,1990,2000), 3.0), color="red", active=T)))
添加query圖例
upset(movies, query.legend = "top", queries = list(list(query = intersects,params = list("Drama", "Comedy", "Action"), color = "orange", active = T,query.name = "Funny action"), list(query = intersects, params = list("Drama"),color = "red", active = F), list(query = intersects, params = list("Action","Drama"), active = T, query.name = "Emotional action")))
參數(shù)attribute.plots
主要是用于添加屬性圖,內(nèi)置有柱形圖、散點(diǎn)圖、熱圖等
柱形圖
upset(movies, main.bar.color = "black", queries = list(list(query = intersects,params = list("Drama"), active = T)), attribute.plots = list(gridrows = 50,plots = list(list(plot = histogram, x = "ReleaseDate", queries = F), list(plot = histogram,x = "AvgRating", queries = T)), ncols = 2))
散點(diǎn)圖
箱線圖
箱線圖可以展示數(shù)據(jù)的分布,通過參數(shù)boxplot.summary控制,最多可以一次性顯示兩個(gè)箱線圖
upset(movies, boxplot.summary = c("AvgRating", "ReleaseDate"))
還有一個(gè)十分重要的功能Incorporating Set Metadata這里就不講了,有興趣的可以參考這份文檔
SessionInfosessionInfo()## R version 3.4.2 (2017-09-28)## Platform: x86_64-w64-mingw32/x64 (64-bit)## Running under: Windows 10 x64 (build 15063)#### Matrix products: default#### locale:## [1] LC_COLLATE=Chinese (Simplified)_China.936## [2] LC_CTYPE=Chinese (Simplified)_China.936## [3] LC_MONETARY=Chinese (Simplified)_China.936## [4] LC_NUMERIC=C## [5] LC_TIME=Chinese (Simplified)_China.936#### attached base packages:## [1] stats graphics grDevices utils datasets methods base#### other attached packages:## [1] UpSetR_1.3.3#### loaded via a namespace (and not attached):## [1] Rcpp_0.12.13 knitr_1.17 magrittr_1.5 munsell_0.4.3## [5] colorspace_1.3-2 rlang_0.1.2 stringr_1.2.0 highr_0.6## [9] plyr_1.8.4 tools_3.4.2 grid_3.4.2 gtable_0.2.0## [13] htmltools_0.3.6 yaml_2.1.14 lazyeval_0.2.0 rprojroot_1.2## [17] digest_0.6.12 tibble_1.3.4 gridExtra_2.3 ggplot2_2.2.1## [21] evaluate_0.10.1 rmarkdown_1.6 labeling_0.3 stringi_1.1.5## [25] compiler_3.4.2 scales_0.5.0 backports_1.1.1
- 蜜度索驥:以跨模態(tài)檢索技術(shù)助力“企宣”向上生長(zhǎng)
- Fortinet李宏凱:2025年在中國(guó)大陸啟動(dòng)SASE PoP節(jié)點(diǎn)部署 助力企業(yè)出海
- Fortinet李宏凱:2024年Fortinet全球客戶已超80萬
- 央國(guó)企采購(gòu)管理升級(jí),合合信息旗下啟信慧眼以科技破局難點(diǎn)
- Apache Struts重大漏洞被黑客利用,遠(yuǎn)程代碼執(zhí)行風(fēng)險(xiǎn)加劇
- Crunchbase:2024年AI網(wǎng)絡(luò)安全行業(yè)風(fēng)險(xiǎn)投資超過26億美元
- 調(diào)查報(bào)告:AI與云重塑IT格局,77%的IT領(lǐng)導(dǎo)者視網(wǎng)絡(luò)安全為首要挑戰(zhàn)
- 長(zhǎng)江存儲(chǔ)發(fā)布聲明:從無“借殼上市”意愿
- 泛微·數(shù)智大腦Xiaoe.AI正式發(fā)布,千人現(xiàn)場(chǎng)體驗(yàn)數(shù)智化運(yùn)營(yíng)場(chǎng)景
- IDC:2024年第三季度北美IT分銷商收入增長(zhǎng)至202億美元
- AI成為雙刃劍!凱捷調(diào)查:97%組織遭遇過GenAI漏洞攻擊
免責(zé)聲明:本網(wǎng)站內(nèi)容主要來自原創(chuàng)、合作伙伴供稿和第三方自媒體作者投稿,凡在本網(wǎng)站出現(xiàn)的信息,均僅供參考。本網(wǎng)站將盡力確保所提供信息的準(zhǔn)確性及可靠性,但不保證有關(guān)資料的準(zhǔn)確性及可靠性,讀者在使用前請(qǐng)進(jìn)一步核實(shí),并對(duì)任何自主決定的行為負(fù)責(zé)。本網(wǎng)站對(duì)有關(guān)資料所引致的錯(cuò)誤、不確或遺漏,概不負(fù)任何法律責(zé)任。任何單位或個(gè)人認(rèn)為本網(wǎng)站中的網(wǎng)頁(yè)或鏈接內(nèi)容可能涉嫌侵犯其知識(shí)產(chǎn)權(quán)或存在不實(shí)內(nèi)容時(shí),應(yīng)及時(shí)向本網(wǎng)站提出書面權(quán)利通知或不實(shí)情況說明,并提供身份證明、權(quán)屬證明及詳細(xì)侵權(quán)或不實(shí)情況證明。本網(wǎng)站在收到上述法律文件后,將會(huì)依法盡快聯(lián)系相關(guān)文章源頭核實(shí),溝通刪除相關(guān)內(nèi)容或斷開相關(guān)鏈接。