R - 群组社交网络分析,为边缘添加权重
R - Group social network analysis, adding weights to edges
我正在尝试创建一个简单的物种社交网络以及它们与其他物种的互动。我已经清理并编码了所有数据,使其具有 3 列(从、到、权重),第一列是观察到的物种,第二列是相关物种,权重是该事件被观察到的次数。我终其一生都无法弄清楚如何让边缘在视觉上代表重量列。我已经尝试了 igraph 和 visNetwork(见下文)。
, , Add weight to edge in Network, and 中有一些漂亮的答案,但唉,它们都在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
您可以在绘图时使用 weight
属性作为边宽,例如,
graph_from_data_frame(data) %>%
plot(edge.width = 5 * E(.)$weight / max(E(.)$weight))
我正在尝试创建一个简单的物种社交网络以及它们与其他物种的互动。我已经清理并编码了所有数据,使其具有 3 列(从、到、权重),第一列是观察到的物种,第二列是相关物种,权重是该事件被观察到的次数。我终其一生都无法弄清楚如何让边缘在视觉上代表重量列。我已经尝试了 igraph 和 visNetwork(见下文)。
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
您可以在绘图时使用 weight
属性作为边宽,例如,
graph_from_data_frame(data) %>%
plot(edge.width = 5 * E(.)$weight / max(E(.)$weight))