SCI繪圖-網(wǎng)絡(luò)圖可視化最詳細(xì)教程!

大家好,我是半島小鐵盒!在上期內(nèi)容?? SCI繪圖-網(wǎng)絡(luò)(Graph)圖可視化工具合集 ??中,介紹了網(wǎng)絡(luò)圖的原理和基本組成以及常用的網(wǎng)絡(luò)圖的可視化工具推薦,那么本期內(nèi)容將為大家?guī)砭W(wǎng)絡(luò)圖可視化分析實(shí)戰(zhàn),通過講解igraph的基本使用讓大家進(jìn)一步理解網(wǎng)絡(luò)圖(Graph)的概念,最后使用ggraph來繪制好看的網(wǎng)絡(luò)圖。
Tips:后續(xù)文中提及的 Nodes 或 Verticles 都表示節(jié)點(diǎn),頂點(diǎn)與節(jié)點(diǎn)同義。
安裝依賴包
使用R包安裝命令`install.packages`分別安裝 igraph 和 ggraph依賴包
install.packages("igraph")install.packages("ggraph")
igraph 包的基本使用
?通過 igraph 來理解 Graph 圖中的節(jié)點(diǎn)(Nodes)、邊(Edges)、布局(Layouts)、度(Degree)以及 節(jié)點(diǎn)和邊的重要性等。
導(dǎo)包
library(igraph)
創(chuàng)建 Graph 1.常見方式
eg. 構(gòu)建一個(gè)包含10個(gè)節(jié)點(diǎn)(編號(hào)為 1 到 10)和連接節(jié)點(diǎn)1-2 和1-5的兩條邊的網(wǎng)絡(luò)
g <- make_graph(edges = c(1,2, 1,5), n=10, directed = FALSE)plot(g)
使用內(nèi)置的網(wǎng)絡(luò)數(shù)據(jù),創(chuàng)建 Graph
g <- make_graph('Zachary')plot(g)

公式以 ~ 開頭, edge操作符使用 + 或 - 表示,其中 - 為無向邊,+ 表示箭頭,‘-+’ 等價(jià)于 ‘->’ 。
g <- make_graph(~ 1-2, 1-5, 3, 4, 5, 6, 7, 8, 9, 10)plot(g)

節(jié)點(diǎn)和邊在igraph中具有數(shù)字ID,對(duì)于具有n個(gè)節(jié)點(diǎn)的圖,節(jié)點(diǎn)ID始終介于1和n之間,邊ID始終介于1和m(圖中邊的總數(shù))之間。
g <- make_graph('Zachary')
添加節(jié)點(diǎn)和邊
# 添加3個(gè)新的節(jié)點(diǎn)g <- add_vertices(g, 3)g <- add_edges(g, edges = c(1,35, 1,36, 34,37))plot(g)

在igraph中可以使用magrittr包中的運(yùn)算符 %>%
# 添加節(jié)點(diǎn)和邊g <- g %>%add_edges(edges=c(1,34)) %>%add_vertices(3) %>%add_edges(edges=c(38,39, 39,40, 40,38, 40,37))
刪除節(jié)點(diǎn)和邊,使用delete_vertices 和delete_edges 來執(zhí)行刪除操作
節(jié)點(diǎn)和邊的其他屬性# 例如刪除連接頂點(diǎn)1-34的邊,獲取其ID然后刪除ids <- get.edge.ids(g, c(1,34))g <- delete_edges(g, ids)
除了ID之外,頂點(diǎn)和邊還具有具有名稱、繪圖坐標(biāo)、元數(shù)據(jù)和權(quán)重等屬性
# 使用公式法創(chuàng)建圖g <- make_graph(~ Alice-Bob:Claire:Frank,Claire-Alice:Dennis:Frank:Esther,George-Dennis:Frank, Dennis-Esther)plot(g)

V 和 E 分別是獲取所有頂點(diǎn)和邊的標(biāo)準(zhǔn)方法
> V(g)+ 7/7 vertices, named, from 8419d0d:[1] Alice Bob Claire Frank Dennis Esther George
> E(g)+ 9/9 edges from 8419d0d (vertex names):[1] Alice --Bob Alice --Claire Alice --Frank Claire--Frank Claire--Dennis[6] Claire--Esther Frank --George Dennis--Esther Dennis--Georg
設(shè)置節(jié)點(diǎn)和邊的屬性值
度 DegreeV(g)$age <- c(25, 31, 18, 23, 47, 22, 50)V(g)$gender <- c("f", "m", "f", "m", "m", "f", "m")E(g)$is_formal <- c(FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE)
> summary(g)IGRAPH 8419d0d UN-- 7 9 --+ attr: name (v/c), age (v/n), gender (v/c), is_formal (e/l)
# 刪除節(jié)點(diǎn)的age屬性g <- delete_vertex_attr(g, "gender"
度指的是節(jié)點(diǎn)所連接的邊的數(shù)量
介數(shù)中心性 找出對(duì)網(wǎng)絡(luò)重要的邊> degree(g)Alice Bob Claire Frank Dennis Esther George3 1 4 3 3 2 2# get by ID or namedegree(g, v=c(3,4,5))degree(g, v=c("Carmina", "Frank", "Dennis"))
布局> ebs <- edge_betweenness(g)> as_edgelist(g)[ebs == max(ebs), ][,1] [,2][1,] "Alice" "Bob"[2,] "Alice" "Claire"
以下是igraph中的所有布局算法,建議提前了解下,在ggraph中也支持igraph中的布局。
| 布局名稱 | 算法描述 |
|---|---|
layout_randomly |
Places the vertices completely randomly |
layout_in_circle |
Deterministic layout that places the vertices on a circle |
layout_on_sphere |
Deterministic layout that places the vertices evenly on the surface of a sphere |
layout_with_drl |
The Drl (Distributed Recursive Layout) algorithm for large graphs |
layout_with_fr |
Fruchterman-Reingold force-directed algorithm |
layout_with_kk |
Kamada-Kawai force-directed algorithm |
layout_with_lgl |
The LGL (Large Graph Layout) algorithm for large graphs |
layout_as_tree |
Reingold-Tilford tree layout, useful for (almost) tree-like graphs |
layout_nicely |
Layout algorithm that automatically picks one of the other algorithms based on certain properties of the graph |
layout <- layout_with_kk(g)plot(g, layout = layout,main = "Social network with the Kamada-Kawai layout algorithm")

繪圖時(shí)節(jié)點(diǎn)、邊的顏色、大小有很多參數(shù)可以調(diào)控,這里就不一一介紹了,具體可參見包的幫助文檔。
# set style of nodesV(g)$color <- ifelse(V(g)$gender == "m", "yellow", "red")plot(g, layout = layout, vertex.label.dist = 3.5,main = "Social network - with genders as colors")

# set edge widthplot(g, layout=layout, vertex.label.dist=3.5, vertex.size=20,vertex.color=ifelse(V(g)$gender == "m", "yellow", "red"),edge.width=ifelse(E(g)$is_formal, 5, 1))

使用 ggraph 包 繪制網(wǎng)絡(luò)圖
ggraph 是 ggplot2 的擴(kuò)展,旨在支持關(guān)系數(shù)據(jù)結(jié)構(gòu),例如網(wǎng)絡(luò)、圖和樹??梢允褂胓gplot2來繪圖,十分輕量和簡(jiǎn)便。
ggraph 需要結(jié)合 tidygraph來使用,tidygraph中 的 tbl_graph 類是 igraph 對(duì)象的封裝,用來加載網(wǎng)絡(luò)圖的數(shù)據(jù),不僅提供使用 tidy API 操作的方法,而且作為igraph 的子類可以調(diào)用父類的方法。
載包library(ggraph)library(tidygraph)
使用 tidygraph 的 as_tbl_graph 方法加載繪圖數(shù)據(jù)
# 內(nèi)置數(shù)據(jù)集 highschool> class(highschool)[1] "data.frame"
> head(highschool)from to year1 1 14 19572 1 15 19573 1 21 19574 1 54 19575 1 55 19576 2 21 1957
# 生成 tbl_graph對(duì)象用于后續(xù)繪圖> gr <- as_tbl_graph(highschool)> gr# A tbl_graph: 70 nodes and 506 edges## A directed multigraph with 1 component
tbl_graph 可以被視為兩個(gè)鏈接表(Nodes 和 Edges)的集合,因此有必要指定在操作期間引用哪個(gè)表, 使用activate 函數(shù)定義當(dāng)前是否正在操作節(jié)點(diǎn)數(shù)據(jù)或邊數(shù)據(jù),其中默認(rèn)激活的是節(jié)點(diǎn)數(shù)據(jù)。
gr <- gr |># 在Nodes表中添加一列 degree數(shù)據(jù)mutate(degree = centrality_degree(mode = 'in'))
使用ggraph繪制第一張網(wǎng)絡(luò)圖
# 采用 igraph中相同的 Kamada-Kawai force-directed algorithm布局ggr <- ggraph(gr, layout = 'kk') +# 繪制透明度0.8且?guī)б稽c(diǎn)弧度的邊geom_edge_fan(aes(alpha = 0.8)) +# 節(jié)點(diǎn)的大小表示degreegeom_node_point(aes(size = degree)) +# 按照年份 切分子圖facet_edges(~year) +# 設(shè)置主題theme_graph(foreground = 'orange', fg_text_colour = 'white')

ggr +geom_edge_fan0(aes(colour = year), alpha = 0.8,show.legend = FALSE) +geom_node_point(aes(size = degree, colour = factor(name)),show.legend = FALSE) +geom_node_text(aes(label=name), repel = T)+facet_edges(~year) +theme_graph(foreground = 'orange', fg_text_colour = 'white')

更換布局
# 設(shè)置 marginset_graph_style(plot_margin = margin(1,1,1,1))# linear layoutggraph(graph, layout = 'linear') +geom_edge_arc(aes(colour = factor(year))) +geom_node_point(size=1) +theme_graph(fg_text_colour = 'white')

circular layout
ggraph(graph, layout = 'linear', circular = TRUE) +geom_edge_arc(aes(colour = from), alpha=0.7) +scale_edge_colour_identity() +geom_node_point(aes(size = degree, colour = factor(name)),show.legend = FALSE) +theme_graph(fg_text_colour = 'white')

partition circular lay o ut
ggraph(graph, 'partition', circular = TRUE) +geom_node_arc_bar(aes(fill = name), size = 0.25,show.legend = FALSE) +scale_fill_viridis_d() +coord_fixed()

circlepack lay o ut
graph <- tbl_graph(flare$vertices, flare$edges)set.seed(1)ggraph(graph, 'circlepack', weight = size) +geom_node_circle(aes(fill = depth), size = 0.25, n = 50,show.legend = FALSE) +scale_fill_viridis_c() +coord_fixed() +theme_graph(fg_text_colour = 'white')
tr eemap lay o ut
ggraph(graph, 'treemap', weight = size) +geom_node_tile(aes(fill = name), size = 0.25,show.legend = FALSE) +scale_fill_manual(values = jet.colors(252)) +theme_graph(fg_text_colour = 'white')

dendrogram lay o ut
dendrogram <- hclust(dist(iris[, 1:4]))ggraph(dendrogram, 'dendrogram', height = height) +geom_edge_elbow() +theme_graph(fg_text_colour = 'white')

ggraph(dendrogram, 'dendrogram', circular = TRUE) +geom_edge_elbow() +coord_fixed()+theme_graph(fg_text_colour = 'white')


網(wǎng)絡(luò)圖可視化小結(jié) >>>
網(wǎng)絡(luò)圖的可視化包和繪圖方法五花八門,只要掌握其中的邏輯操作起來都是千篇一律!當(dāng)然,圖形的美觀這方面的調(diào)節(jié)還是需要個(gè)人天賦,實(shí)在不行找篇文獻(xiàn)的樣圖模仿便是了!ggplot2提供了很棒的底層方法,讓網(wǎng)絡(luò)圖的繪制輕量簡(jiǎn)便。只要你肯費(fèi)些功夫,一定能繪制出好看的SCI圖 。
點(diǎn)贊??關(guān)注
本期就介紹到這里啦!持續(xù)更新ING~
整理不易,期待您的點(diǎn)贊、分享、轉(zhuǎn)發(fā)...
往期精彩推薦
SCI繪圖-網(wǎng)絡(luò)(Graph)圖可視化工具合集 龍年微信紅包封面!限量2000份! 科研論文還不知道如何配色2? 科研論文還不知道如何配色1? SCI繪圖-ggplot繪制超好看的火山圖 HTTPX,一個(gè)超強(qiáng)的爬蟲庫(kù)! GSEA分析實(shí)戰(zhàn)及結(jié)果可視化教程! 一文讀懂GSEA分析原理 SCI繪圖-箱線圖的繪制原理與方法
更多合集推薦
??#SCI繪圖 ?? #生物信息 ? #科研繪圖素材資源 ?? #數(shù)學(xué) ?? #測(cè)序原理
我知道你
在看
哦
