1

I have the following network graph:

library(tidyverse)
library(igraph)


set.seed(123)
n=15
data = tibble(d = paste(1:n))

relations = tibble(
  from = sample(data$d),
  to = lead(from, default=from[1]),
)

graph = graph_from_data_frame(relations, directed=T, vertices = data) 

V(graph)$color <- ifelse(data$d == relations$from[1], "red", "orange")

plot(graph, layout=layout.circle, edge.arrow.size = 0.2)

enter image description here

I am trying to see how to remove "edges" from this graph.

I found the following code to remove "edges" (https://igraph.org/r/doc/igraph-minus.html):

#remove the "edge" between "node 14 and node 10"
g = graph
g <- g - edge("14|10")
plot(g)

Is there a quick way to remove all "edges" in this graph?

for (i in 1:15) {
    for (j in 1:15) {
       g <- g - edge("i|j")
    }
}  

But this is not working:

Error in as.igraph.vs(graph, vp) : Invalid vertex names

Is there a better way then to remove all the edges one by one?

Thank you!

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
stats_noob
  • 5,401
  • 4
  • 27
  • 83

3 Answers3

1

If you want to remove all edges, you can use

g <- graph-E(graph)
plot(g)

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thank you! Do you know why my "loop" is not working? – stats_noob Feb 23 '22 at 06:49
  • Because base R doesn't have interpolated strings. When you say `edge("i|j")` it looks for nodes named "i" and "j". It doesn't know that those are variable names with other values. You'd have to paste in the values with something like `edge(paste0(i, "|", j))` – MrFlick Feb 23 '22 at 06:50
  • do you mean like this? – stats_noob Feb 23 '22 at 06:55
  • for (i in 1:15) { for (j in 1:15) { g <- g - edge(paste0(i, "|", j)) } } – stats_noob Feb 23 '22 at 06:55
  • @antonoyaro8 that's the right syntax, but the problem is that you'll get an error if you try to choose an edge that doesn't exist. So you'd need to make sure it's there before removing. – MrFlick Feb 23 '22 at 07:00
1

If you want to create a graph without any edges, you can try the code below

make_empty_graph(15)
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Thank you! I was looking to first make a graph, and then delete edges - this looks like a cool function for directly creating an empty graph. Thank you! – stats_noob Feb 24 '22 at 16:07
  • Right now I am working on these related questions: https://stackoverflow.com/questions/71243676/directly-adding-titles-and-labels-to-visnetwork – stats_noob Feb 24 '22 at 16:07
  • https://stackoverflow.com/questions/71244872/zoom-and-hide-options-for-html-widgets – stats_noob Feb 24 '22 at 16:08
0
g - E(g) -> g

will remove all edges from g.

clp
  • 1,098
  • 5
  • 11