R語言實(shí)現(xiàn)高級的韋恩圖可視化(UpSetR)

hello,大家好屡限,不知道下面這張圖大家還有印象么

圖片.png

在我之前分享的文章10X單細(xì)胞(10X空間轉(zhuǎn)錄組)之細(xì)胞通訊軟件之間的分析比較中,大量采用這樣的圖進(jìn)行展示,后來呢归敬,在文章文章中的啞鈴圖復(fù)現(xiàn)(基礎(chǔ)知識)嘗試完全實(shí)現(xiàn),可惜半路停止,今天就給大家展示如何實(shí)現(xiàn)這樣的圖,不帶一點(diǎn)折扣妹懒。

其實(shí)這個圖有個專業(yè)的名詞,叫韋恩圖双吆,但是大家熟悉的韋恩圖應(yīng)該是長下面的樣子:

圖片.png

少量的情況下非常美觀眨唬,但數(shù)據(jù)一多信息展示就會混亂,于是就有了上面的展示方式好乐,好了匾竿,借助R包UpSet,我們來實(shí)現(xiàn)這個圖蔚万。

接下來我們直接通過實(shí)例來看下如何進(jìn)行數(shù)據(jù)的可視化:

install.packages("UpSetR")
##載入包
library(UpSetR)
library(ggplot2)
library(grid)
library(plyr)
##構(gòu)建數(shù)據(jù)

# example oflist input (list of named vectors)
listInput <-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))
 
# example ofexpression input
expressionInput<- c(one = 2, two = 1, three = 2, `one&two` = 1, `one&three` = 4,`two&three` = 1, `one&two&three` = 2)
##可視化結(jié)果
upset(fromList(listInput),order.by = "freq")
圖片.png
upset(fromExpression(expressionInput),order.by = "freq")
圖片.png
##載入數(shù)據(jù)并繪圖
movies <-read.csv(system.file("extdata", "movies.csv", package ="UpSetR"), header = T, sep = ";")
 
## nsets(頻數(shù)最多的前六個變量)岭妖,text.scale =c(intersection size title, intersection size ticklabels, set size title, set size tick labels, set names, numbers above bars)
upset(movies,nsets = 6, number.angles = 30, point.size = 3.5, line.size = 2, mainbar.y.label = "Genre Intersections", sets.x.label = "Movies Per Genre", text.scale =c(1.3, 1.3, 1, 1, 2, 0.75))
圖片.png
##自定義交集的組
upset(movies,sets = c("Action", "Adventure", "Comedy","Drama", "Mystery", "Thriller","Romance", "War", "Western"), mb.ratio = c(0.55,0.45), order.by = "freq")
圖片.png
##基于相交的等級進(jìn)行排序
upset(movies,sets = c("Action", "Adventure", "Comedy","Drama", "Mystery", "Thriller","Romance", "War", "Western"), mb.ratio = c(0.55,0.45), order.by = "degree")
圖片.png
##基于等級和頻率共同排序,通過先后來確定排序順序
upset(movies,sets = c("Action", "Adventure", "Comedy","Drama", "Mystery", "Thriller","Romance", "War", "Western"), mb.ratio = c(0.55,0.45), order.by = c("degree", "freq"))
圖片.png
##保留各組頻數(shù)反璃,不排序
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)
圖片.png
##對交集結(jié)果進(jìn)行分組昵慌,nintersects交叉點(diǎn)的數(shù)目閾值,cutoff交叉結(jié)果閾值版扩。
upset(movies,nintersects = 70, group.by = "sets", cutoff = 7)
圖片.png
##空的交叉點(diǎn)展示
upset(movies,empty.intersections = "on", order.by = "freq")
圖片.png
##利用不同的顏色顯示重要的信息
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)))
圖片.png
##通過設(shè)置閾值進(jìn)行標(biāo)記
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)))
圖片.png
##通過expression進(jìn)行篩選
upset(movies,queries = list(list(query = intersects, params = list("Action", "Drama"),active = T), list(query = elements, params = list("ReleaseDate", 1980,1990, 2000), color = "red", active = F)), expression ="AvgRating > 3 & Watches > 100")
圖片.png
##自定義的query結(jié)構(gòu)
Myfunc <-function(row, release, rating) {data <- (row["ReleaseDate"] %in%release) & (row["AvgRating"] > rating)}#row數(shù)據(jù)源废离,release侄泽,rating指的是parms中的第一礁芦,二個參數(shù)
upset(movies,queries = list(list(query = Myfunc, params = list(c(1970, 1980, 1990, 1999,2000), 2.5), color = "blue", active = T)))
圖片.png
##增加query的標(biāo)簽legend
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 = "Emotionalaction")))
圖片.png
##綜合前面的方式的完整例子
upset(movies, query.legend = "bottom", queries =list(list(query = Myfunc, params = list(c(1970,
    1980, 1990, 1999, 2000),2.5), color = "orange", active = T), list(query = intersects,
    params = list("Action","Drama"), active = F), list(query = elements, params =list("ReleaseDate",
    1980, 1990, 2000), color ="red", active = F, query.name = "Decades")),
    expression ="AvgRating > 3 & Watches > 100")
圖片.png
##通過柱狀圖增加變量的其它數(shù)據(jù)信息其中type=bar plot("hist") or heat map ("heat"/“bool”)
sets <- names(movies[3:19])
avgRottenTomatoesScore <- round(runif(17, min = 0, max = 90))
metadata <- as.data.frame(cbind(sets, avgRottenTomatoesScore))
names(metadata) <- c("sets","avgRottenTomatoesScore")
metadata$avgRottenTomatoesScore <-as.numeric(as.character(metadata$avgRottenTomatoesScore))
upset(movies, set.metadata = list(data = metadata, plots =list(list(type = "hist",
    column ="avgRottenTomatoesScore", assign = 20))))
圖片.png
##增加熱圖信息
Cities <- sample(c("Boston", "NYC","LA"), 17, replace = T)
metadata <- cbind(metadata, Cities)
metadata$Cities <- as.character(metadata$Cities)
metadata[which(metadata$sets %in% c("Drama","Comedy", "Action", "Thriller",
    "Romance")), ]
upset(movies, set.metadata = list(data = metadata, plots =list(list(type = "heat",
    column ="Cities", assign = 10, colors = c(Boston = "green", NYC ="navy",
        LA ="purple")))))
圖片.png
##增加文字信息
upset(movies, set.metadata = list(data = metadata, plots =list(list(type = "text",
    column ="Cities", assign = 10, colors = c(Boston = "green", NYC ="navy",
        LA ="purple")))))
圖片.png
##直接設(shè)置連線區(qū)域背景
upset(movies, set.metadata = list(data = metadata, plots =list(list(type = "hist",
    column ="avgRottenTomatoesScore", assign = 20), list(type ="matrix_rows",
    column ="Cities", colors = c(Boston = "green", NYC ="navy", LA = "purple"),
    alpha = 0.5))))
圖片.png
##一次添加多種信息
accepted <- round(runif(17, min = 0, max = 1))
metadata <- cbind(metadata, accepted)
metadata[which(metadata$sets %in% c("Drama","Comedy", "Action", "Thriller", "Romance")),]
upset(movies, set.metadata = list(data = metadata, plots =list(list(type = "hist", column = "avgRottenTomatoesScore",assign = 20), list(type = "bool", column = "accepted", assign= 5, colors = c("#FF3333", "#006400")), list(type ="text", column = "Cities", assign = 5, colors = c(Boston ="green", NYC = "navy", LA = "purple")))))
圖片.png
##混合圖的繪制,通過attribute.plots添加
upset(movies, set.metadata = list(data = metadata, plots =list(list(type = "hist",
    column ="avgRottenTomatoesScore", assign = 20), list(type = "bool",column = "accepted",
    assign = 5, colors =c("#FF3333", "#006400")), list(type = "text",column = "Cities",
    assign = 5, colors =c(Boston = "green", NYC = "navy", LA ="purple")),
    list(type ="matrix_rows", column = "Cities", colors = c(Boston ="green",
        NYC ="navy", LA = "purple"), alpha = 0.5))), queries =list(list(query = intersects,
    params =list("Drama"), color = "red", active = F), list(query =intersects,
    params =list("Action", "Drama"), active = T), list(query =intersects,
    params =list("Drama", "Comedy", "Action"), color ="orange", active = T)),
    attribute.plots =list(gridrows = 45, plots = list(list(plot = scatter_plot,
        x ="ReleaseDate", y = "AvgRating", queries = T), list(plot =scatter_plot,
        x ="AvgRating", y = "Watches", queries = F)), ncols = 2),query.legend = "bottom")
圖片.png
##自定義繪圖函數(shù)的混合繪圖

myplot <- function(mydata, x, y) {
    plot <- (ggplot(data =mydata, aes_string(x = x, y = y, colour = "color")) +
        geom_point() + scale_color_identity()+ theme(plot.margin = unit(c(0,
        0, 0, 0),"cm")))
}
 
another.plot <- function(data, x, y) {
    data$decades <-round_any(as.integer(unlist(data[y])), 10, ceiling)
    data <-data[which(data$decades >= 1970), ]
    myplot <- (ggplot(data,aes_string(x = x)) + geom_density(aes(fill = factor(decades)),
        alpha = 0.4) +theme(plot.margin = unit(c(0, 0, 0, 0), "cm"), legend.key.size =unit(0.4,
        "cm")))
}
upset(movies, main.bar.color = "black", queries =list(list(query = intersects,
    params =list("Drama"), color = "red", active = F), list(query =intersects,
    params =list("Action", "Drama"), active = T), list(query =intersects,
    params =list("Drama", "Comedy", "Action"), color ="orange", active = T)),
    attribute.plots =list(gridrows = 45, plots = list(list(plot = myplot, x ="ReleaseDate",
        y ="AvgRating", queries = T), list(plot = another.plot, x ="AvgRating",
        y ="ReleaseDate", queries = F)), ncols = 2))
圖片.png
##增加箱線圖
upset(movies, boxplot.summary = c("AvgRating","ReleaseDate"))
圖片.png

好了悼尾,大家多多學(xué)習(xí)

生活很好柿扣,有你更好

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者闺魏。
  • 序言:七十年代末未状,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子析桥,更是在濱河造成了極大的恐慌司草,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件泡仗,死亡現(xiàn)場離奇詭異埋虹,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)娩怎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門搔课,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人截亦,你說我怎么就攤上這事爬泥〖硖郑” “怎么了?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵袍啡,是天一觀的道長踩官。 經(jīng)常有香客問我,道長境输,這世上最難降的妖魔是什么卖鲤? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任,我火速辦了婚禮畴嘶,結(jié)果婚禮上蛋逾,老公的妹妹穿的比我還像新娘。我一直安慰自己窗悯,他們只是感情好区匣,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蒋院,像睡著了一般亏钩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上欺旧,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天姑丑,我揣著相機(jī)與錄音,去河邊找鬼辞友。 笑死栅哀,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的称龙。 我是一名探鬼主播留拾,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鲫尊!你這毒婦竟也來了痴柔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤疫向,失蹤者是張志新(化名)和其女友劉穎咳蔚,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體搔驼,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡谈火,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了匙奴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片堆巧。...
    茶點(diǎn)故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出谍肤,到底是詐尸還是另有隱情啦租,我是刑警寧澤,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布荒揣,位于F島的核電站篷角,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏系任。R本人自食惡果不足惜恳蹲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望俩滥。 院中可真熱鬧嘉蕾,春花似錦、人聲如沸霜旧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挂据。三九已至以清,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間崎逃,已是汗流浹背掷倔。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留个绍,地道東北人勒葱。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓,卻偏偏與公主長得像障贸,于是被迫代替她去往敵國和親错森。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容

  • 具體內(nèi)容移步我的博客 [圖片上傳失敗...(image-3ecfcc-1517035636075)] 簡介 集合可...
    taoyan閱讀 8,381評論 1 25
  • 09.韋恩圖繪制 清除當(dāng)前環(huán)境中的變量 設(shè)置工作目錄 gplots包繪制韋恩圖 VennDiagram包繪制韋恩圖
    Davey1220閱讀 26,836評論 1 16
  • 前言 上一節(jié)篮洁,我們介紹了如何繪制韋恩圖來顯示集合間的交疊關(guān)系 但是,隨著集合的增多殃姓,韋恩圖顯示的關(guān)系會越來越復(fù)雜袁波,...
    名本無名閱讀 1,430評論 0 15
  • 終于超過1k粉絲啦~之前說要小小慶祝一下,害怕直接放文章里會被封文章蜗侈,所以福利大家可以見評論篷牌。前幾周忙著答辯畢業(yè)事...
    jlyq617閱讀 21,011評論 10 55
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭踏幻,有人歡樂有人憂愁枷颊,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,521評論 28 53