gpt4 book ai didi

r - 将边列表加权到 r 中的有向和无向图中

转载 作者:行者123 更新时间:2023-12-02 17:14:51 25 4
gpt4 key购买 nike

我有以下加权边列表 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) 一个无向图,对所有交互进行求和(权重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 没有按照我的想法去做(表示没有链接)

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

最佳答案

请考虑以下选项。

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

关于r - 将边列表加权到 r 中的有向和无向图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46977769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com