r中有向无向图的加权边列表
我有以下加权边缘列表r中有向无向图的加权边列表,r,igraph,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)
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)我使用
我认为发生的事情是,weight=0
没有做我认为应该做的事情(表示没有链接)
我也不确定为什么节点不在页面上展开[
,请考虑下面的选项。################################################################### 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)
来自码码码码码码码码码码码码码方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方第一种选择
如果我有一个非常大的数据库(N=500万),那么选项一的自动化程度会降低,对吗?我可能遗漏了什么。另外,在我的图中,我希望显示隔离,这就是为什么我使用
weight==0
。选项2似乎删除了隔离,这个假设是否错误?是的,对于如此大的数据库,第二个选项更合适。此选项(以及选项1)不会删除孤立节点;它删除权重=0的链接。在本例中,您可以看到节点1003
即使没有链接也仍然存在于网络中。我收到了一个代码错误:directed\u graph\u wgt2它是delete\u edges
而不是delete.edges`对于我来说,至少使用igraph
还有as.undirected(directed\u graph\u wgt,mode=“collapse”,edge.attr.comb=“sum”)
当我有一个日期变量时,行错误被清除。当然,最初的版本忽略了这一点,但是如果程序能够忽略这个变量或者至少将它指向weight列,我将不胜感激
undirected_graph_wgt <- as.undirected(directed_graph, mode = "collapse", edge.attr.comb = "sum")
plot(undirected_graph_wgt)
################################################################### 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)