美文网首页生信绘图
R可视化:igraph的分组展示

R可视化:igraph的分组展示

作者: 生信学习者2 | 来源:发表于2020-12-16 09:31 被阅读0次

    使用igraph画网络图最大的优势是避免手动修图,最近正好看到如何分组展示网络图的代码,特记录下来。更多知识分享请到 https://zouhua.top/

    导入网络数据

    library(igraph)
    library(igraphdata)
    
    #I load the network
    data(karate)
    #for reproducible purposes
    set.seed(123)
    karateLayout <- layout_with_fr(karate)
    par(mar = c(0,0,2,0))
    

    分组展示

    # 计算网络分组属性
    Communitykarate <- cluster_louvain(karate)
    prettyColors <- c("turquoise4", "azure4", "olivedrab","deeppink4")
    communityColors <- prettyColors[membership(Communitykarate)]
    
    # 根据网络分组属性重新计算weights
    edge.weights <- function(community, network, weight.within = 100, weight.between = 1) {
      bridges <- crossing(communities = community, graph = network)
      weights <- ifelse(test = bridges, yes = weight.between, no = weight.within)
      return(weights) 
    }
    E(karate)$weight <- edge.weights(Communitykarate, karate)
    # I use the original layout as a base for the new one
    karateLayoutA <- layout_with_fr(karate, karateLayout)
    # the graph with the nodes grouped
    plot(x = Communitykarate, 
         y = karate, 
         edge.width = 1, 
         vertex.size = 10, 
         mark.groups = NULL, 
         layout = karateLayoutA, 
         vertex.label = NA, 
         col = communityColors, 
         c("darkgrey","tomato2")[crossing(Communitykarate, karate) + 1],
         main = "Communities in Zachary's karate club network (grouped)")
    

    修改组间聚类距离

    E(karate)$weight <- edge.weights(Communitykarate, karate, weight.within = 1000)
    karateLayoutB <- layout_with_fr(karate, karateLayout)
    plot(x = Communitykarate, 
         y = karate, 
         edge.width = 1, 
         vertex.size = 10,
         mark.groups = NULL, 
         layout = karateLayoutB, 
         vertex.label = NA, 
         col = communityColors, 
         c("darkgrey","tomato2")[crossing(Communitykarate, karate) + 1],
         main = "Communities in Zachary's karate club network (grouped)")
    

    circle下的分组展示

    karate_groups <- cluster_optimal(karate) # cluster_fast_greedy(karate)
    coords <- layout_in_circle(karate, order =
              order(membership(karate_groups)))
    V(karate)$label <- sub("Actor ", "", V(karate)$name)
    V(karate)$label.color <- membership(karate_groups)
    V(karate)$shape <- "none"
    plot(karate, layout = coords)
    

    R information

    sessionInfo()
    
    R version 4.0.2 (2020-06-22)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows 10 x64 (build 19042)
    
    Matrix products: default
    
    locale:
    [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
    [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
    [5] LC_TIME=English_United States.1252    
    system code page: 936
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
    [1] igraphdata_1.0.1 psych_2.0.9      igraph_1.2.5     tibble_3.0.3     dplyr_1.0.2     
    
    loaded via a namespace (and not attached):
     [1] pillar_1.4.6     compiler_4.0.2   tools_4.0.2      digest_0.6.25    jsonlite_1.7.1   evaluate_0.14   
     [7] lifecycle_0.2.0  nlme_3.1-150     lattice_0.20-41  pkgconfig_2.0.3  rlang_0.4.7      cli_2.1.0       
    [13] rstudioapi_0.11  yaml_2.2.1       parallel_4.0.2   xfun_0.19        stringr_1.4.0    knitr_1.30      
    [19] generics_0.1.0   vctrs_0.3.4      grid_4.0.2       tidyselect_1.1.0 glue_1.4.2       R6_2.5.0        
    [25] fansi_0.4.1      rmarkdown_2.5    purrr_0.3.4      magrittr_1.5     ellipsis_0.3.1   htmltools_0.5.0 
    [31] assertthat_0.2.1 mnormt_2.0.2     stringi_1.5.3    tmvnsim_1.0-2    crayon_1.3.4    
    

    参考

    1. 如何在igraph中进行分组展示

    参考文章如引起任何侵权问题,可以与我联系,谢谢。

    相关文章

      网友评论

        本文标题:R可视化:igraph的分组展示

        本文链接:https://www.haomeiwen.com/subject/whhngktx.html