(R语言)理解什么是“a”;加权;图表

(R语言)理解什么是“a”;加权;图表,r,graph,data-visualization,cluster-analysis,nodes,R,Graph,Data Visualization,Cluster Analysis,Nodes,我正在使用R和igraph库学习网络图数据。特别是,我试图理解“加权图”的概念——从我所读到的内容来看,“权重”通常与图中的“边”相关联。但“权重”能与“节点”相关联吗?(有时,我看到“节点”也被称为“顶点”) 假设我有两个数据集:一个用于节点,一个用于边 library(igraph) library(visNetwork) Nodes <-data.frame( "Source" = c("123","124","

我正在使用R和igraph库学习网络图数据。特别是,我试图理解“加权图”的概念——从我所读到的内容来看,“权重”通常与图中的“边”相关联。但“权重”能与“节点”相关联吗?(有时,我看到“节点”也被称为“顶点”)

假设我有两个数据集:一个用于节点,一个用于边

library(igraph)
library(visNetwork)

Nodes <-data.frame(

"Source" = c("123","124","125","122","111", "126"),

"Salary" = c("100","150","200","200","100", "100"),

"Debt" = c("10","15","20","20","10", "10"),

"Savings" = c("1000","1500","2000","2000","1000", "1000")

)

Nodes$Salary= as.numeric(Nodes$Salary)
Nodes$Debt = as.numeric(Nodes$Debt)
Nodes$Savings = as.numeric(Nodes$Savings)


mydata <-data.frame(

"source" = c("123","124","123","125","123"),
"target" = c("126", "123", "125", "122", "111"),
"color" = c("red","red","green","blue","red"),
"food" = c("pizza","pizza","cake","pizza","cake")
)
现在,我们需要创建一个新的图表,其中平均财务信息被视为“权重”。这就是我开始感到困惑的地方

这种方法不起作用:

set_vertex_attr(simple_graph, Weight, index = V(graph), nodes_avg$Means)

Error in as.igraph.vs(graph, index) : 
      Cannot use a vertex sequence from another graph.
我尝试了以下命令,但收到一条警告消息:

E(simple_graph)$weight <- nodes_avg$Means

Warning message:
In eattrs[[name]][index] <- value :
  number of items to replace is not a multiple of replacement length

E(simple_graph)$weight我认为有一些社区检测算法可以考虑“边缘权重”。我不确定社区算法是否可以考虑“节点权重”,谢谢!我会记住这一点。人们通常谈论节点或边的属性。节点属性可以是薪水、身高或任何你喜欢的,一个常见的边属性是权重,但节点权重不是一个简单的概念。它们通常计算为节点边的权重和。对于聚类算法,Igraph中考虑了边缘权重属性的社团检测算法。对于节点权重(或任意任意节点属性)的聚类,可以考虑非规则方法,如规则分类。
set_vertex_attr(simple_graph, Weight, index = V(graph), nodes_avg$Means)

Error in as.igraph.vs(graph, index) : 
      Cannot use a vertex sequence from another graph.
E(simple_graph)$weight <- nodes_avg$Means

Warning message:
In eattrs[[name]][index] <- value :
  number of items to replace is not a multiple of replacement length
weighted_graph <- graph_from_data_frame(mydata, directed=TRUE, vertices=nodes_avg)
simple_weighted_graph<- simplify(weighted_graph)
plot(simple_weighted_graph)


#do some clustering on the weighted_graph#
 fc <- fastgreedy.community(simple_weighted_graph)
 V(simple_weighted_graph)$community <- fc$membership
 nodes <- data.frame(id = V(simple_weighted_graph)$name, title = V(simple_weighted_graph)$name, group = V(simple_weighted_graph)$community)
 nodes <- nodes[order(nodes$id, decreasing = F),]
 edges <- get.data.frame(simple_weighted_graph, what="edges")[1:2]

 visNetwork(nodes, edges) %>%
     visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)