??弧線圖(Arc Diagrams)是一種在一維布局中可視化網(wǎng)絡(luò)的方法策菜,主要特征是沿單軸顯示節(jié)點又憨,同時用弧線表示節(jié)點之間的邊或連接锭吨,同時邊的粗細可以表示連接的權(quán)重。特別是在想展示同一條染色體上基因之間的關(guān)聯(lián)性時躏将,用此圖絕不失為一個好的方法考蕾。
安裝 arcdiagram
??首先肖卧,安裝一下專門用于畫弧線圖的包arcdiagram
,該包在github上沒有發(fā)布版本拦赠,所以可以通過devtools
來安裝:
library(devtools)
install_github('gastonstat/arcdiagram')
示例數(shù)據(jù)
??這里使用的數(shù)據(jù)來自arcdiagram
包的github里面矛紫,是一個gml格式的文件牌里,gml是igraph對象的一種存儲格式,我們可以用igraph
包來讀仍:
library(igraph)
# 下載數(shù)據(jù)
download.file('https://github.com/gastonstat/arcdiagram/blob/master/lesmiserables.gml', 'lesmiserables.gml')
mis_graph <- read.graph('lesmiserables.gml', format="gml")
# 查看網(wǎng)絡(luò)對象
mis_graph
IGRAPH bc6aae4 U--- 77 254 --
+ attr: id (v/n), label (v/c), group (v/n), fill (v/c), border (v/c),
| value (e/n)
+ edges from bc6aae4:
[1] 1-- 2 1-- 3 1-- 4 3-- 4 1-- 5 1-- 6 1-- 7 1-- 8 1-- 9 1--10
[11] 11--12 4--12 3--12 1--12 12--13 12--14 12--15 12--16 17--18 17--19
[21] 18--19 17--20 18--20 19--20 17--21 18--21 19--21 20--21 17--22 18--22
[31] 19--22 20--22 21--22 17--23 18--23 19--23 20--23 21--23 22--23 17--24
[41] 18--24 19--24 20--24 21--24 22--24 23--24 13--24 12--24 24--25 12--25
[51] 25--26 24--26 12--26 25--27 12--27 17--27 26--27 12--28 24--28 26--28
[61] 25--28 27--28 12--29 28--29 24--30 28--30 12--30 24--31 31--32 12--32
+ ... omitted several edges
# 準備繪圖數(shù)據(jù)
# 獲取邊
edgelist <- get.edgelist(mis_graph)
# 獲取頂點標簽
vlabels <- get.vertex.attribute(mis_graph, "label")
# 獲取頂點分組信息
vgroups <- get.vertex.attribute(mis_graph, "group")
# 獲取頂點填充色
vfill <- get.vertex.attribute(mis_graph, "fill")
# 獲取頂點邊的顏色
vborders <- get.vertex.attribute(mis_graph, "border")
# 獲取頂點權(quán)重
degrees <- degree(mis_graph)
# 獲取邊權(quán)重
values <- get.edge.attribute(mis_graph, "value")
??簡單介紹一下igraph對象挺尿,例如上面的mis_graph
就是一個igraph對象编矾。我們可以從該對象的概要里面獲取到如下信息:bc6aae4
是這個對象的名稱窄俏,U--- 77 254 --
這里的U(亦或DNW碘菜,表示有向忍啸、有命名且加權(quán)的網(wǎng)絡(luò))表示該對象包含一個無向網(wǎng)絡(luò)计雌,后面數(shù)字表示該網(wǎng)絡(luò)有77個節(jié)點和254條邊白粉。該對象具有的節(jié)點屬性是id鼠渺、group、fill鹃祖、border恬口,具有的邊屬性是value祖能。最下面的內(nèi)容就是該對象具體包含的關(guān)系對蛾洛。
繪圖
??準備好數(shù)據(jù)养铸,繪圖就水到渠成了钞螟,只需執(zhí)行arcplot
函數(shù)即可鳞滨,參數(shù)還是很多的,大家可以根據(jù)需要自行調(diào)整拯啦。
library(arcdiagram)
arcplot(edgelist, labels=vlabels, cex.labels=0.8,
show.nodes=TRUE, col.nodes=vborders, bg.nodes=vfill,
cex.nodes = log(degrees)+0.5, pch.nodes=21,
lwd.nodes = 1, line= 0.5,
col.arcs = hsv(0, 0, 0.2, 0.25), lwd.arcs = 1.2 * values)
結(jié)構(gòu)如下:
排序
??當然仙蛉,我們也可以先對數(shù)據(jù)排個序值骇,比如按照分組信息排序班缎,這樣一個分組內(nèi)的頂點就會挨在一起按照大小排列显熏,這樣更有助于數(shù)據(jù)之間的比較喷兼。
x <- data.frame(vgroups, degrees, vlabels, ind=1:vcount(mis_graph))
y <- x[order(x$vgroups, x$degrees, decreasing=T), ]
new_ord <- y$ind
arcplot(edgelist, ordering=new_ord, labels=vlabels, cex.labels=0.8,
show.nodes=TRUE, col.nodes=vborders, bg.nodes=vfill,
cex.nodes = log(degrees)+0.5, pch.nodes=21,
lwd.nodes = 1, line=0.5,
col.arcs = hsv(0, 0, 0.2, 0.25), lwd.arcs = 1.2 * values)
結(jié)果如下:
結(jié)束語
??arcdiagram
包繪圖還是挺容易的,可能準備好相應(yīng)的數(shù)據(jù)更費事一些季惯,所以有空還是得學習一個igraph對象相關(guān)內(nèi)容吠各。好了,繪圖的事到此為止勉抓。說點別的贾漏,比如能發(fā)這個帖子至少說明小編今天還沒有“變聲成羊”。不得不說藕筋,新冠的傳染性還真是厲害纵散,不到一周的時間辦公室里面七八成的人都感染了。雖然毒性沒有那么強隐圾,但是沒法預(yù)測自身感染的癥狀是否激烈伍掀,所以一些退燒止咳緩解疼痛的藥還是要買的。而現(xiàn)實的狀況可能卻是另外一回事暇藏,由于短時間內(nèi)需求的急劇飆升導致供不應(yīng)求蜜笤,一藥難求把兔。哎焰坪,原來布洛芬有這么多種姿勢善绎,奇怪的知識又增加了炬守。此刻,很想練一下久陰真經(jīng)。。辟拷。
參考資料
https://www.r-bloggers.com/2013/02/arc-diagrams-in-r-les-miserables
往期回顧
可視化:circular dendrogram
可視化:和弦圖
LACE-seq 保姆級教程
可視化:兩種方法繪制捎鸾埽基圖
可視化:density與ridgeline