1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 12154字,需瀏覽 25分鐘

         ·

        2024-04-10 15:28

        cd0e88728fe1ba34c69dd8e61135c940.webp

        大家好,我是半島小鐵盒!在上期內(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)
              
            

        29d22b94c572a95d60d30637b97126fd.webp

        使用內(nèi)置的網(wǎng)絡(luò)數(shù)據(jù),創(chuàng)建 Graph

              
                g <- make_graph('Zachary')
              
              
                plot(g)
              
            

        9c6c3dda09bfac6573ae4fc7eecf7fc3.webp

        2.公式表示法

        公式以 ~ 開頭, edge操作符使用 + 或 - 表示,其中 - 為無向邊,+ 表示箭頭,‘-+’  等價(jià)于 ‘->’ 。

              
                g <- make_graph(~ 1-2, 1-5, 3, 4, 5, 6, 7, 8, 9, 10)
              
              
                plot(g)
              
            

        f114d6b90ad6c1cb8c8457b65de868fa.webp

        節(jié)點(diǎn)和邊

        節(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)
              
            

        eb3796f50c2eadd523391cdda591435d.webp

        在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í)行刪除操作

              
                # 例如刪除連接頂點(diǎn)1-34的邊,獲取其ID然后刪除
              
              
                ids <- get.edge.ids(g, c(1,34))
              
              
                g <- delete_edges(g, ids)
              
            
        節(jié)點(diǎn)和邊的其他屬性

        除了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)
              
            

        d806bf2c4cc90116cc5ffacf9b39fdfe.webp

        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)和邊的屬性值

              
                V(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"
        度 Degree
        度指的是節(jié)點(diǎn)所連接的邊的數(shù)量
              
                > degree(g)
              
              
                 Alice    Bob Claire  Frank Dennis Esther George 
              
              
                     3      1      4      3      3      2      2 
              
              
                # get by ID or name
              
              
                degree(g, v=c(3,4,5))
              
              
                degree(g, v=c("Carmina", "Frank", "Dennis"))
              
            
        介數(shù)中心性 找出對(duì)網(wǎng)絡(luò)重要的邊
              
                > 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")
              
            

        e312322a285c02ba05764d6781ba81a4.webp

        繪圖
        繪圖時(shí)節(jié)點(diǎn)、邊的顏色、大小有很多參數(shù)可以調(diào)控,這里就不一一介紹了,具體可參見包的幫助文檔。
              
                
                  # set style of nodes
                
              
              
                V(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")
              
            

        8d6093c18c8e78c5866cae777b1c7737.webp

              
                
                  # set edge width
                
              
              
                plot(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))
              
            

        3b0ef0a60254834eb8ae1fb9d1418f60.webp


        使用 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 year 1 1 14 1957 2 1 15 1957 3 1 21 1957 4 1 54 1957 5 1 55 1957 6 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)的大小表示degree
              
              
                    geom_node_point(aes(size = degree)) + 
              
              
                    # 按照年份 切分子圖
              
              
                    facet_edges(~year) + 
              
              
                    # 設(shè)置主題
              
              
                    theme_graph(foreground = 'orange', fg_text_colour = 'white')
              
            

        47c15aa78719c65f980aed4eeeaeeb4b.webp

        添加節(jié)點(diǎn)標(biāo)簽和顏色
              
                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')
              
            

        b5cb80665c458a5d617f57db7b8989c6.webp

        更換布局

        linear layout
              
                
                  # 設(shè)置 margin
                
              
              
                set_graph_style(plot_margin = margin(1,1,1,1))
              
              
                
                  # linear layout
                
              
              
                ggraph(graph, layout = 'linear') + 
              
              
                  geom_edge_arc(aes(colour = factor(year))) +
              
              
                  geom_node_point(size=1) + 
              
              
                  theme_graph(fg_text_colour = 'white')
              
            

        f0d87f02c8105edcb2cff4f24d7367c3.webp

        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')
              
            

        8e0493ed4452b6100444f15959a95a06.webp

        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()
              
            

        aa0b09b5a885f1c684273dd34787bf92.webp

        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')
              
            

        d56e2a916cf06161d22482031404269d.webp

        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')
              
            

        93187cfd91c2595b2499028fa07556b3.webp

        dendrogram lay o ut

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

        ce18175b38b52697d6d0bb025a1774da.webp

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

        950db2fb28d1ae288330c503f721d33a.webp

        此外,Node和Edge也有很多的樣式選擇。當(dāng)然,tidygraph還提供了很多算法來查詢網(wǎng)絡(luò)中的節(jié)點(diǎn)和計(jì)算邊、節(jié)點(diǎn)的重要性!

        77012e994d4f67df569ba5b713eb4d0e.webp

        不過多贅述了,推薦結(jié)合官方參考學(xué)習(xí)!

        網(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圖



        f49d630d09d41ccd26fb0febbeb62f2b.webp

        點(diǎn)贊??關(guān)注

        本期就介紹到這里啦!持續(xù)更新ING~

        f996bf4fc0c4eb346e69661e0a12fa85.webp

        整理不易,期待您的點(diǎn)贊、分享、轉(zhuǎn)發(fā)...



        33b483530af2dca878a55e0f05742f72.webp

        往期精彩推薦

        SCI繪圖-網(wǎng)絡(luò)(Graph)圖可視化工具合集 龍年微信紅包封面!限量2000份! 科研論文還不知道如何配色2? 科研論文還不知道如何配色1? SCI繪圖-ggplot繪制超好看的火山圖 HTTPX,一個(gè)超強(qiáng)的爬蟲庫(kù)! GSEA分析實(shí)戰(zhàn)及結(jié)果可視化教程! 一文讀懂GSEA分析原理 SCI繪圖-箱線圖的繪制原理與方法
        a0e49ba6503a49715229bc19daa9c26e.webp

        更多合集推薦

        ??#SCI繪圖  ?? #生物信息  ? #科研繪圖素材資源  ?? #數(shù)學(xué)  ?? #測(cè)序原理

        我知道你

        在看

        77218a2c0ce5ce3aea5401212faec5f0.webp
        瀏覽 70
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            色开心激情网 | 草逼免费看 | 男女抽搐视频 | 久久久久99精品成人网站 | 波多野结衣做爱视频 | 欧洲成人在线观看 | 欧美成人精品A A A | 太深了好疼轻点小说 | 久久产精品 | 欧美不卡视频在线 |