【问题标题】:Issues with plotting network in igraph在 igraph 中绘制网络的问题
【发布时间】:2026-02-18 17:25:03
【问题描述】:

我在使用库 igraph 在 R 中实现二分网络时遇到了一些问题。这是我的脚本:

library(igraph)
library(reshape2)
setwd("....")
getwd()
library(readxl)
network=read_excel("network1.xlsx")
print(network)
subjects=as.character(unlist(network[,1]))
agents=colnames(network[-1])
print(network)
network = network[,-1]
g=graph.incidence(network, weighted = T)
V(g)$type
V(g)$name=c(subjects,agents)
V(g)$color = V(g)$type
V(g)$color=gsub("FALSE","red",V(g)$color)
V(g)$color=gsub("TRUE","lightblue",V(g)$color)
plot(g, edge.arrow.width = 0.3,
     vertex.size = 5, 
     edge.arrow.size = 0.5,
     vertex.size2 = 5,
     vertex.label.cex = 1,
     vertex.label.color="black",
     asp = 0.35, 
     margin = 0,
     edge.color="grey",
     edge.width=(E(g)$weight),
     layout=layout_as_bipartite)

网络绘制正确

如你所见

但是我有两个问题

(1) 我不明白图中顶点的显示顺序。它们与excel文件的顺序不同,既不是字母顺序也不是数字顺序。它们似乎是随机排列的。如何选择顶点的放置顺序?

(2) 我不明白为什么有些顶点更靠近,而有些则更远。我会在相同距离的所有顶点。我该怎么办?

非常感谢您的宝贵帮助。

【问题讨论】:

  • 我们没有您的文件network1.xlsx,因此我们无法运行您的示例。为了帮助我们帮助您,请运行您的代码来创建变量网络(直到并包括行 network = network[,-1])。然后运行 ​​dput(network) 并将结果粘贴到您的问题中,以便我们为您的示例提供帮助。

标签: r plot igraph reshape2


【解决方案1】:

由于你没有提供你的数据,我将用一个虚构的例子来说明。

图表数据示例

library(igraph)
set.seed(123)
EL = matrix(c(sample(8,18, replace=T),
    sample(LETTERS[1:6], 18, replace=T)), ncol=2)
g = simplify(graph_from_edgelist(EL))
V(g)$type = bipartite_mapping(g)$type 
VCol = c("#FF000066", "#0000FF66")[as.numeric(V(g)$type)+1]
plot(g, layout=layout_as_bipartite(g), vertex.color=VCol)

与您的图表一样,这有两个问题。节点是任意排序的 下排的间距很奇怪。让我们一次一个地解决这些问题。 为此,我们需要控制布局,而不是使用任何 自动布局功能。布局只是一个vcount(g) * 2 矩阵 给出用于绘图的顶点的 x-y 坐标。这里我放一个 通过将 y 坐标指定为 1 和其他 通过指定 y=0 在下一行中的节点。我们要横向指定顺序 按每组内的排名(按字母顺序)。所以

LO = matrix(0, nrow=vcount(g), ncol=2)
LO[!V(g)$type, 2] = 1
LO[V(g)$type, 1]  = rank(V(g)$name[V(g)$type]) 
LO[!V(g)$type, 1] = rank(V(g)$name[!V(g)$type])
plot(g, layout=LO, vertex.color=VCol)

现在两行都是有序且均匀分布的,但是因为更少 在底行的顶点,有一种不吸引人的、不平衡的外观。我们 可以通过拉伸底行来解决这个问题。我发现做正确的事更容易 如果坐标从 0 变为(节点数)的比例因子 - 1 而不是 1 到(节点数)如上。这样做,我们得到

LO[V(g)$type, 1]  = rank(V(g)$name[V(g)$type]) - 1
LO[!V(g)$type, 1] = (rank(V(g)$name[!V(g)$type]) - 1) * 
    (sum(V(g)$type) - 1)  /  (sum(!V(g)$type) - 1)
plot(g, layout=LO, vertex.color=VCol)

【讨论】:

    【解决方案2】:

    非常感谢。我执行了您非常有用的示例,并且在第一步中,我使用我的数据正常工作,保持边缘的不同厚度以及我的情节中的所有内容,但顺序正确。这非常重要,非常感谢。但是,我在理解如何用我的数据正确地重新调整顶行和底行时遇到了一些麻烦,因为它们似乎总是太近了。可能我没有完全理解我必须工作的坐标。这是我的数据。

    > `> network=read_excel("network1.xlsx",2)
    > dput(network)
    structure(list(`NA` = c(2333, 2439, 2450, 2451, 2452, 2453, 2454, 
    2455, 2456, 2457, 2458, 2459, 2460, 2461, 2480, 2490, 2491, 2492, 
    2493, 2494, 2495), A = c(12, 2, 2, 5, 2, 0, 5, 3, 0, 0, 7, 0, 
    0, 0, 6, 2, 10, 7, 1, 2, 5), B = c(0, 1, 0, 1, 0, 0, 2, 0, 0, 
    0, 0, 0, 1, 0, 5, 0, 2, 0, 0, 0, 0), C = c(0, 0, 0, 0, 1, 0, 
    4, 0, 0, 0, 0, 1, 0, 0, 2, 0, 4, 4, 2, 1, 0), D = c(2, 0, 0, 
    0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 7, 0, 4, 0, 1, 4, 0), E = c(11, 
    2, 3, 3, 3, 8, 3, 6, 4, 1, 1, 0, 12, 0, 5, 0, 4, 6, 4, 8, 9), 
        F = c(2, 0, 0, 3, 1, 0, 10, 1, 0, 0, 0, 1, 0, 0, 9, 0, 0, 
        1, 1, 3, 3), G = c(0, 3, 1, 1, 0, 0, 0, 0, 0, 3, 2, 0, 0, 
        0, 1, 0, 0, 2, 0, 1, 0), H = c(0, 0, 2, 0, 0, 0, 1, 0, 0, 
        0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 1), I = c(0, 0, 0, 0, 0, 
        0, 3, 0, 6, 3, 0, 0, 1, 0, 7, 0, 0, 4, 1, 2, 0), J = c(0, 
        0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
        0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -21L), .Names = c(NA, "A", "B", "C", "D", "E", "F", "G", "H", 
    "I", "J"))
    > print(network)
         NA  A B C D  E  F G H I J
    1  2333 12 0 0 2 11  2 0 0 0 0
    2  2439  2 1 0 0  2  0 3 0 0 0
    3  2450  2 0 0 0  3  0 1 2 0 0
    4  2451  5 1 0 0  3  3 1 0 0 0
    5  2452  2 0 1 0  3  1 0 0 0 0
    6  2453  0 0 0 0  8  0 0 0 0 1
    7  2454  5 2 4 2  3 10 0 1 3 0
    8  2455  3 0 0 0  6  1 0 0 0 0
    9  2456  0 0 0 0  4  0 0 0 6 0
    10 2457  0 0 0 0  1  0 3 0 3 0
    11 2458  7 0 0 0  1  0 2 0 0 0
    12 2459  0 0 1 0  0  1 0 0 0 0
    13 2460  0 1 0 0 12  0 0 0 1 0
    14 2461  0 0 0 0  0  0 0 0 0 0
    15 2480  6 5 2 7  5  9 1 2 7 1
    16 2490  2 0 0 0  0  0 0 0 0 0
    17 2491 10 2 4 4  4  0 0 0 0 0
    18 2492  7 0 4 0  6  1 2 0 4 0
    19 2493  1 0 2 1  4  1 0 0 1 0
    20 2494  2 0 1 4  8  3 1 0 2 0
    21 2495  5 0 0 0  9  3 0 1 0 0
    > `
    

    【讨论】:

    • 请不要将要求澄清或后续问题作为答案。为此使用 cmets。您应该通过editing it将您的数据添加到您的问题帖子中并删除此答案帖子。