R-小组社交网络分析,将权重增加到边缘

发布于 2025-01-23 04:14:49 字数 2695 浏览 3 评论 0原文

我正在尝试建立一个简单的物种社交网络及其与其他物种的相互作用。我已经清理并编码了所有数据以具有3列(从,重量),第一列是观察到的物种,第二列是关联物种,重量是观察到该事件的次数。我一生无法弄清楚如何使边缘在视觉上表示重量列。我已经尝试了igraph和visnetwork(见下文)。

如何在某些边缘增加特定的权重? /a>, Plot网络图网络边缘的权重python 在网络中增加重量 , and plotting graph with igraph in R: edge长度与重量成正比,但可惜,它们都在Python中。任何建议都非常感谢!

DPUT数据 -

data <- structure(list(from = c(5L, 5L, 5L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), to = c(1L, 2L, 3L, 5L, 4L, 2L, 
3L, 1L, 2L, 3L, 5L, 1L, 4L, 3L, 5L, 1L, 4L, 2L), weight = c(8, 
20, 9, 8, 10, 416, 121, 9, 26, 21, 19, 430, 28, 210, 7, 111, 
20, 203)), row.names = c(NA, -18L), class = "data.frame")

输出 -

from     to       weight
<int>    <int>    <dbl>
5        1       8      
5        2       20     
5        3       9      
1        5       8      
1        4       10     
1        2       416        
1        3       121        
4        1       9      
4        2       26     
4        3       21     
2        5       19     
2        1       430        
2        4       28     
2        3       210        
3        5       7      
3        1       111        
3        4       20     
3        2       203    

我使用新的节点和边缘列表在IGRAPH中制作图形 - 需要分离一些软件包信息,以确保使用正确的软件包。

library(igraph)
library("igraph", quietly = TRUE, warn.conflicts = FALSE, verbose = FALSE)

igraphroutes <- graph_from_data_frame(d = data, vertices = nodes, directed = F)
igraphroutes
plot(igraphroutes)

df_edges <- as_data_frame(igraphroutes, what = "edges")
df_edges <- df_edges[order(df_edges$weight),]
new_graph <- graph_from_data_frame(d = df_edges, vertices = as_data_frame(igraphroutes, what = "vertices"))

E(new_graph)$weight

plot(new_graph)

我还尝试在Visnetwork中工作

require(visNetwork, quietly = TRUE)
library(visNetwork)
data <- as.data.frame(data) %>%
  mutate(weight = as.numeric(data$weight))

plot(igraphroutes, edge.width=E(igraphroutes)$weight)

visNetwork(nodes, data) %>%
  visEdges(nodes, data) %>%
  visIgraphLayout(layout = "layout_with_fr") -> visual
visual

I am trying to create a simple social network of species and their interactions with other species. I have cleaned and coded all my data to have 3 columns (from, to, weight) with the first column being the observed species, second column being the associating species, and weight being how many times that event was observed. I cannot for the life of me figure out how to get the edges to visually represent the weight column. I've tried both igraph and visNetwork (see below).

There are some beautiful answers in How to add specific weights to some edges?, Plot Network Graph and add edge weights to network edges python, Add weight to edge in Network, and plotting graph with igraph in R: edge length proportional to weight, but alas, they are all in Python. Any advice is greatly appreciated!

dput data -

data <- structure(list(from = c(5L, 5L, 5L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), to = c(1L, 2L, 3L, 5L, 4L, 2L, 
3L, 1L, 2L, 3L, 5L, 1L, 4L, 3L, 5L, 1L, 4L, 2L), weight = c(8, 
20, 9, 8, 10, 416, 121, 9, 26, 21, 19, 430, 28, 210, 7, 111, 
20, 203)), row.names = c(NA, -18L), class = "data.frame")

Output -

from     to       weight
<int>    <int>    <dbl>
5        1       8      
5        2       20     
5        3       9      
1        5       8      
1        4       10     
1        2       416        
1        3       121        
4        1       9      
4        2       26     
4        3       21     
2        5       19     
2        1       430        
2        4       28     
2        3       210        
3        5       7      
3        1       111        
3        4       20     
3        2       203    

I used new nodes and edge list to make graph in igraph - required detaching some package info to ensure correct package was being used.

library(igraph)
library("igraph", quietly = TRUE, warn.conflicts = FALSE, verbose = FALSE)

igraphroutes <- graph_from_data_frame(d = data, vertices = nodes, directed = F)
igraphroutes
plot(igraphroutes)

df_edges <- as_data_frame(igraphroutes, what = "edges")
df_edges <- df_edges[order(df_edges$weight),]
new_graph <- graph_from_data_frame(d = df_edges, vertices = as_data_frame(igraphroutes, what = "vertices"))

E(new_graph)$weight

plot(new_graph)

I also tried working within visNetwork

require(visNetwork, quietly = TRUE)
library(visNetwork)
data <- as.data.frame(data) %>%
  mutate(weight = as.numeric(data$weight))

plot(igraphroutes, edge.width=E(igraphroutes)$weight)

visNetwork(nodes, data) %>%
  visEdges(nodes, data) %>%
  visIgraphLayout(layout = "layout_with_fr") -> visual
visual

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2025-01-30 04:14:50

您可以在绘图时使用stright属性用于边缘宽度,例如,

graph_from_data_frame(data) %>%
    plot(edge.width = 5 * E(.)$weight / max(E(.)$weight))

“在此处输入图像说明”

You can use the weight attribute for edge widths when plotting, e.g.,

graph_from_data_frame(data) %>%
    plot(edge.width = 5 * E(.)$weight / max(E(.)$weight))

enter image description here

夜深人未静 2025-01-30 04:14:50

如果使用Visnetwork,则可以将“重量”列重命名为“值”,并且它将自动表示边缘的宽度与节点之间的相互作用数量成正比。

If using visNetwork, you can rename your "weight" column to "value" and it will automatically represent the width of the edges proportional to the number of interactions between nodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文