gpt4 book ai didi

python - 在 R 中使用 igraph 绘制图形 : edge length proportional to weight

转载 作者:行者123 更新时间:2023-12-01 08:22:06 24 4
gpt4 key购买 nike

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

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

感谢您的帮助!

最佳答案

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

下面的 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 于 2019-02-07 创建(v0.2.1)

关于python - 在 R 中使用 igraph 绘制图形 : edge length proportional to weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54571716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com