【问题标题】:plotting graph with igraph in R: edge length proportional to weight在 R 中用 igraph 绘制图形:边长与权重成正比
【发布时间】:2019-02-07 10:55:47
【问题描述】:

我需要为加权无向图绘制一个简单的图,其中唯一的边在单个中心节点和其他一些节点之间(即星形网络拓扑),所以我只需要我的节点等距(即围绕中心节点的每对连续节点之间的角度相同。但是,我的边缘是加权的,我希望边缘长度与权重值成正比。有没有办法在 R 或 Python 中实现这一点?我一直在研究 igraph 包,但似乎找不到合适的布局。这是我正在使用的示例数据框:

d = data.frame("n1"=c('A','A','A','A'), "n2"=c('B','C','D','E'), "weight"=c(1,1.5,2,5))

感谢您的帮助!

【问题讨论】:

    标签: python r graph igraph


    【解决方案1】:

    我不相信有专门的布局可以做到这一点,但您可以通过创建一组布局坐标然后按您的权重缩放它们以非常简单的方式实现此效果。

    R 中的示例代码如下:

    library(igraph)
    
    # toy data
    d = data.frame("n1"=c('A','A','A','A'), "n2"=c('B','C','D','E'), "weight"=c(1,1.5,2,5))
    
    g <- graph_from_data_frame(d)
    
    # we can create a layout object that is just coordinate positions
    coords <- layout_(g, as_star())
    coords
    #>               [,1]          [,2]
    #> [1,]  0.000000e+00  0.000000e+00
    #> [2,]  1.000000e+00  0.000000e+00
    #> [3,]  6.123234e-17  1.000000e+00
    #> [4,] -1.000000e+00  1.224647e-16
    #> [5,] -1.836970e-16 -1.000000e+00
    
    # Knowing this is a star the N nodes should have N-1 edges. We can scale
    # The N-1 positions by the weights
    weight.scale <- c(1, d$weight)
    
    coords2 <- weight.scale * coords
    coords2
    #>               [,1]          [,2]
    #> [1,]  0.000000e+00  0.000000e+00
    #> [2,]  1.000000e+00  0.000000e+00
    #> [3,]  9.184851e-17  1.500000e+00
    #> [4,] -2.000000e+00  2.449294e-16
    #> [5,] -9.184851e-16 -5.000000e+00
    
    # we can inspect
    plot(g, layout = coords2)
    

    reprex package (v0.2.1) 于 2019 年 2 月 7 日创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 2014-04-07
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多