I am trying to delete an edge from a graph.
I ran the following code
library(igraph)
A <- c(0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0)
dim(A) <- c(5,5)
g1 <- graph_from_adjacency_matrix(t(A), mode = c("directed"))
g1 <- delete_edges(g1, c(1,2))
g1 <- simplify(g1)
coords <- c(0.0, 0.951056, 0.587785, -0.587785, -0.951056,
1, 0.309017, -0.809017,-0.809017, 0.309017)
dim(coords) <- c(5,2)
plot(g1,
vertex.color=c(rgb(0,.5,.7),
rgb(.15,.6,0),
rgb(.5,.5,.5),
rgb(.8,.55,0),
rgb(.8,0,0)),
vertex.size = 20,
layout = coords,
main = paste("Graph", toString(1)))
and produced the following
I would like the 1->2 edge to be deleted but the 2 ->3 edge to remain. Any ideas on what is happening and how to fix it?
the delete_edges
function assumes you are passing in an edge index. You can see the edges with
E(g1)
# + 5/5 edges from faf79f6:
# [1] 1->2 2->3 3->4 4->5 5->1
so when you specify c(1,2)
you are specifying the first and second edges, not the edge that goes form vertex 1 to 2. If you wanted to do that instead, you could do
g1 <- delete_edges(g1, E(g1)[1 %--% 2])
Another way to do this is
g1 <- g1 - edge("1|2")
If you do have a vertex sqeuence that you would like to extract the edges for, you can use the P=
parameter of the E()` function. For example
g1 <- delete_edges(g1, E(g1, P=c(1,2,3,4)))
This will remove the edge that goes from 1 to 2, and 3 to 4.