个人网络中igraph的中心度

时间:2018-10-19 01:11:03

标签: r igraph

我有两列

temp <- read.delim(text = 'columnA columnB tnum
*Mickey Daffy 12345
*Minnie Donald 34567
Huey Minnie 21345
Donald Minnie 22345
Scrooge Mickey 22456', sep = ' ')

我试图与这些人一起创建具有中心度(DoC)的有向图和无向图,以表明Minnie具有较高的DoC。

编辑: 抱歉,需要更多说明:我想创建一个如下图,每个节点都有名称和编号。enter image description here

我也想知道如何展示给定图像之间的中间性。请问下面的行

plot.igraph(g, edge.label = edge_attr(g, "tnum")) 

基于tnum的数量计算边缘权重?-因为我想计算特定tnum的实例/频率并根据其绘制边缘/度。

2 个答案:

答案 0 :(得分:0)

使用webView制作数据帧的图形,然后将图形传递到igraph::graph_from_dataframe

igraph::degree

会给你

dirG <- graph_from_data_frame(temp)
degree(dirG)

答案 1 :(得分:0)

这取决于您所说的“中心度”:图形中的节点具有度,即它与其他节点的链接数;在有向图中,这是度数和度数之和。

此外,您可以计算节点的中心度,这实际上是试图回答以下问题:节点在图中的“良好连接”,“嵌入”或只是“中心”(我是 很清楚,这缩短了许多中心性的概念,但这是StackOverflow,而不是Wikipedia。有许多种不同的中心性,每种都采用不同的方法。同样,维基百科或您最喜欢的图论书籍是您的朋友。

以下代码说明了如何计算节点的度数和中心度:

library(igraph)

df <- data.frame(
  "from" = c("Mickey", "Minnie", "Huey", "Donald", "Scrooge"),
  "to" = c("Daffy", "Donald", "Minnie", "Minnie", "Minnie")
)

g <- graph_from_data_frame(df, directed = TRUE) # or FALSE
g <- set_edge_attr(g, name = "tnum", value = c(12345, 34567, 21345, 22345, 22456))

plot.igraph(g, edge.label = edge_attr(g, "tnum"))

degree(g)
centralization.betweenness(g)

如果您需要其他集中度度量,则可以在the igraph manual中搜索centralitycentralization

如果您想基于这些中心度度量为图形着色,请查看以下代码,如果不清楚,请查阅出色的igraph手册:

library(igraph)

df <- data.frame(
  "from" = c("Mickey", "Minnie", "Huey", "Donald", "Scrooge"),
  "to" = c("Daffy", "Donald", "Minnie", "Minnie", "Minnie")
)

# create an igraph object from the dataframe, which essentially is a list
# of edges between nodes
g <- graph_from_data_frame(df, directed = TRUE) # or FALSE

# each edge receives "tnum" as an attribute
g <-
  set_edge_attr(g,
                name = "tnum",
                value = c(12345, 34567, 21345, 22345, 22456))

# calculate betweenness of nodes and store it in an attribute "color" of the
# vertices. The color-attribute is treated specially when plotting graphs.
V(g)$color <- betweenness(g)

plot.igraph(g,
            # plot edge labels from the "tnum" attribute
            edge.label = edge_attr(g, "tnum"),
            # specify the palette of colours to use when plotting vertices
            palette = heat.colors(n = 99))

# same as above; we multiply by 100 to make sure all values are > 0, otherwise
# the colour will be interpreted as 0 (usually, white)
V(g)$color <- (eigen_centrality(g)$vector) * 100

# assign edge weights based on the last digit of "tnum"
E(g)$weight <- E(g)$tnum %% 10

plot.igraph(
  g,
  edge.label = edge_attr(g, "tnum"),
  edge.width = E(g)$weight,
  edge.arrow.size = .8,
  palette = heat.colors(n = 99)
)