简体   繁体   English

如何查看 R igraph 中权重最大的两个节点?

[英]How do I see the two nodes of most weighted egdes in R igraph?

I have an igraph.network object constructed in R and generated weight information for each edge.我有一个在 R 中构建的 igraph.network object 并为每条边生成权重信息。 I want to see the nodes of the most weighted edges (descending).我想查看权重最大的边(降序)的节点。 What codes should I use to do that?我应该使用什么代码来做到这一点? Thank you!谢谢!

# create an igraph project of user interaction network and check descriptives.

library(igraph)

#edge list
EL = read.csv("(file path omitted)user_interaction_structure.csv")
head(EL)

#node list: I do not have a node list

#construct an igraph oject
g <- graph_from_data_frame(EL, directed = TRUE, vertices = NULL)

#check the edge and node number of the network
gsize(g)
vcount(g)

#check nodes based on degree (descending)
deg <- igraph::degree(g)
dSorted <-sort.int(deg,decreasing=TRUE,index.return=FALSE)
dSorted

#check edges based on weight
E(g)

#the network will contain loop edges and multiple edges
#simplify multiple edges
g_simple <- graph.adjacency(get.adjacency(g),weighted=TRUE)

#check edge weight
E(g_simple)$weight

#igraph can generate a matrix
g_simple[]

Then I wanted to see who were interacting heavily with whom (the nodes of the edges with the largest weight),so I tried然后我想看看谁和谁有大量交互(边上权重最大的节点),所以我尝试了

e_top_weights <- order(order(E(g_simple))$weight, decreasing=TRUE)

but it did not work.但它没有用。

I think what you want is the igraph function strength() , which gives the sum of the weights of the edges incident to each node.我想你想要的是 igraph function strength() ,它给出了每个节点的边缘权重之和。 Here's an example:这是一个例子:

library(igraph)

                                        # A small graph we can visualize
g <- make_ring(5)
                                        # Assign each edge an increasing weight, to make things
                                        # easy
edgeweights<- 1:ecount(g)
E(g)$weight <- edgeweights
                                        # The strength() function sums the weights of edges incident
                                        # to each node
strengths <- strength(g)
                                        # We can collect the top two strengths by sorting the
                                        # strengths vector, then asking for which elements of the
                                        # strengths vector are equal to or greater than the second
                                        # largest element.
toptwo <- which(strengths >= sort(strengths, decreasing = TRUE)[2])
## [1] 4 5
                                        # Assign nodes a color blue that is more saturated when nodes
                                        # have greater strength.
cr <- colorRamp(c(rgb(0,0,1,.1), rgb(0,0,1,1)), alpha = TRUE)
colors <- cr(strengths/max(strengths))
V(g)$color <- apply(colors, 1, function(row) rgb(row[1], row[2], row[3], row[4], maxColorValue = 255))
                                        # Plot to confirm
plot(g, edge.width = edgeweights)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM