Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove unconnected nodes R igraph or ggnet

I have a symmetric matrix that represent degree of connections among actors. I would like to cancel out the vertex unconnected.

The functions included in igraph (as delete_edges or delete_vertices) do not work for my case. I share my code

#import of matrix
matrix3<-import("matrix2a.xlsx")
r.name <- matrix2a [,1]
rownames(matrix2a) <- r.name
matrix2a <- matrix2a %>% select(-X__1)
View(matrix2a)
m=as.matrix(matrix2a)
#I compute the maximum spanning tree graph
g <- graph_from_adjacency_matrix(m, mode = "upper", weighted = T, diag = F)
max_spann_tree <- mst(g, weights = -E(g)$weight)
#I obtain a network with some unconneted vertex that I would like to erase

thanks in advance for your help!

like image 619
Leonardo Avatar asked Oct 17 '25 04:10

Leonardo


1 Answers

I am not sure what you mean "The functions included in igraph (as delete_edges or delete_vertices) do not work for my case." delete.vertices is made for precisely this purpose.

Since you do not provide data, I will show a small example from random data. I am adding labels to the graph so that the numbering will not change when I delete the isolated vertices. I am also using an explicit layout so I can lay out the reduced graph in the same way for comparison.

library(igraph)
set.seed(1234)
G = erdos.renyi.game(40, 0.055)
V(G)$label=1:40     
LO = layout_with_fr(G)
plot(G, layout = LO)

Graph 01

Now identify the isolated vertices and use delete.vertices to remove them.

Isolated = which(degree(G)==0)
G2 = delete.vertices(G, Isolated)
LO2 = LO[-Isolated,]
plot(G2, layout=LO2)

Graph 02

The same graph but without the isolated vertices.

If this is not what you want, please be more explicit about why it does not work for your graph.

like image 157
G5W Avatar answered Oct 19 '25 20:10

G5W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!