简体   繁体   English

在 r 中将边列表加权为有向图和无向图

[英]weighted edgelist into directed and undirected graph in r

I have the following weighted edgelist el我有以下加权edgelist 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))

I want to create a few visualizations: (1) a directed graph where the weights are used to thicken the lines between two given nodes, (2) an undirected graph that sums all of the interactions ( weight s) together and produces the same graph, and graphs that recreate (1) and (2) without using the weights.我想创建一些可视化:(1)一个有向图,其中权重用于加粗两个给定节点之间的线,(2)一个无向图,将所有交互( weight )加在一起并生成相同的图,以及在不使用权重的情况下重新创建 (1) 和 (2) 的图。

for (1) I use对于(1)我使用

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

but the resulting graph does has a line on 1003 which is wrong because in the data they have no interactions但结果图在1003上确实有一条线,这是错误的,因为在数据中它们没有交互

I have a similar issue with the undirected我对 undirected 有类似的问题

 undirected_graph_wgt <- as.undirected(directed_graph, mode = "collapse", edge.attr.comb = "sum")
plot(undirected_graph_wgt)

I think what is going on is that weight=0 is not doing what I think it is (signifying no link)我认为正在发生的事情是weight=0没有做我认为的事情(表示没有链接)

Also I am not sure why the nodes are not spread out on the page另外我不确定为什么节点没有在页面上展开

Please, consider the following options.请考虑以下选项。

################################################################### 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM