将加权边缘列表转换为r

时间:2017-10-27 14:46:00

标签: r igraph

我有以下加权边缘列表el

structure(list(from = c(1001L, 1001L, 1002L, 1002L, 1003L, 1003L
), to = c(1002L, 1003L, 1001L, 1003L, 1001L, 1002L), weight = c(2L, 
0L, 1L, 0L, 0L, 0L)), .Names = c("from", "to", "weight"), class = "data.frame", row.names = c(NA, 
-6L))

我想创建一些可视化:(1)有向图,其中权重用于加粗两个给定节点之间的线,(2)一个无向图,它对所有交互进行求和(weight s )一起生成相同的图形,以及在不使用权重的情况下重新创建(1)和(2)的图形。

表示(1)我使用

directed_graph_wgt <- graph.data.frame(el, directed = TRUE)
plot(directed_graph,layout=layout.fruchterman.reingold,edge.width=E(directed_graph)$weight/2)

但结果图在1003上有一行是错误的,因为在数据中他们没有互动

我与无向的

有类似的问题
 undirected_graph_wgt <- as.undirected(directed_graph, mode = "collapse", edge.attr.comb = "sum")
plot(undirected_graph_wgt)

我认为发生的事情是weight=0没有做我认为的事情(表示没有链接)

此外,我不确定为什么节点没有在页面上展开

1 个答案:

答案 0 :(得分:2)

请考虑以下选项。

################################################################### FIRST OPTION

el <- data.frame(from = c(1001L, 1001L, 1002L, 1002L, 1003L, 1003L),
                 to = c(1002L, 1003L, 1001L, 1003L, 1001L, 1002L),
                 weight = c(2L, 0L, 1L, 0L, 0L, 0L))
el
# This design of edgelist implies that there are links between (i) 1003 and 1001
# and between (ii) 1003 and 1002 but the weight of those links is 0
library(igraph)
directed_graph_wgt <- graph.data.frame(el, directed = TRUE)
# the adjacency matrix of your network looks like this
adj.mat <- as_adjacency_matrix(directed_graph_wgt, type = "both", names = TRUE,
                               sparse = FALSE)
adj.mat # as you can see, the adjacency_matrix shows links between 
# (i) 1003 and 1001 and and between (ii) 1003 and 1002

                                ### IMPORTANT ### 
# When you move from an adjacency matrix to a graph, you might consider
# that a "weight" of 0 in the matrix leads to no interaction (edge) between nodes. 
# But be careful,  the adjacency matrix is actually a relationnal matrix
# (or connection matrix), not just a weights matrix. It actually provides the 
# number of edges between nodes from the graph.
# Therefore, when you are designing your graph.data.frame, your must include 
# ONLY connected nodes

# Something like this should be relevent here
nodes <-  c(1001L, 1002L, 1003L) # create a vector of nodes 
nodes # we have three nodes
el <- data.frame(from = c(1001L, 1002L),
                 to = c(1002L, 1001L),
                 weight = c(2L, 1L)) # use ONLY connected nodes
el # structrure of links
# Now, you can include all the nodes in the network with the "vertices" argument

# (1) directed graph
directed_graph_wgt <- graph.data.frame(el, directed = TRUE,
                                       vertices = nodes)
# the right adjacency matrix of your network looks like this
adj.mat <- as_adjacency_matrix(directed_graph_wgt, type = "both", names = TRUE,
                               sparse = FALSE)
adj.mat # as you can see, the adjacency matrix shows all the nodes 
# and here is the plot
plot(directed_graph_wgt, layout = layout.fruchterman.reingold,
     edge.width = E(directed_graph_wgt)$weight/2)

# (2) undirected graph
undirected_graph_wgt <- as.undirected(directed_graph_wgt, mode = "collapse",
                                      edge.attr.comb = "sum")
plot(undirected_graph_wgt)


### KEYPOINT
# When you are designing your own edgelist, setting a weight as 0 means that 
# the weight of the considered link is 0, not that the link doesn't exist. 
# However, a 0 in the adjacency matrix leads to no interaction (edge)
# between nodes.

# It looks like you are confusing "weights" and "number of interactions". Even 
# though both can be used interchangeably, you must be careful when using them
# in igraph functions


################################################################## SECOND OPTION

# To use the weights in "el" as indicator of relations, you need to consider
# the assumption that weight == 0 means no link.
# You can use the delete_edges() function

el <- data.frame(from = c(1001L, 1001L, 1002L, 1002L, 1003L, 1003L),
                 to = c(1002L, 1003L, 1001L, 1003L, 1001L, 1002L),
                 weight = c(2L, 0L, 1L, 0L, 0L, 0L))
el #t this is your initial edgelist

# (1) directed graph
directed_graph_wgt <- graph.data.frame(el, directed = TRUE)
directed_graph_wgt
# use delete_edges() to consider ONLY weight != 0 (no link)
# We will remove link with weight == 0 (i.e no link)
directed_graph_wgt2 <- delete_edges(directed_graph_wgt,
                                    which(E(directed_graph_wgt)$weight == 0))
plot(directed_graph_wgt2, layout = layout.fruchterman.reingold,
     edge.width = E(directed_graph_wgt)$weight/2)

# (2) undirected graph
undirected_graph_wgt <- as.undirected(directed_graph_wgt, mode = "collapse",
                                      edge.attr.comb = "sum")
undirected_graph_wgt2 <- delete_edges(undirected_graph_wgt,
                                      which(E(undirected_graph_wgt)$weight == 0))
plot(undirected_graph_wgt2)