0
votes

I need to highlight selected nodes in a plot, with the full graph as semi-transparent background. In plot, I can easily ifelse vertices that fall inside or outside my list of selected nodes, to differentiate their colors. But I haven't been able to do the same with edges. How do I index the edges that connect my selected nodes?

x <- make_ring(10)
get.edge.ids(x, c(1,2))

get.edge.ids works only if you provide an even number vertices, not if you provide a possibly uneven arbitrary number.

This question here is similar to mine, but not the same.

2

2 Answers

0
votes

OK, I found a solution. Not sure if it is the most elegant solution, but it works. If you look inside igraph.es objects, names are stored in the form "name1|name2". So you induce a subgraph containing only your selected verticies, you turn it into an edgelist, and then apply a column merge with a "|" collapse, which gives you the desired "name1|name2"-formatted edges. Then you create an edge color attribute for your target edges.

x <- make_ring(10)
V(x)$name <- LETTERS[1:10]
target <- V(x)[1:5]$name
target <- as_edgelist(induced.subgraph(x, target))
target <- apply(target, 1, function(z) paste(z, collapse = "|"))
E(x)$color <- "blue"
E(x)[target]$color <- "red"
plot(x, edge.color = E(x)$color)

enter image description here

0
votes

The E and V functions have numerous iterators and subsetting functions as described a bit here: http://cneurocvs.rmki.kfki.hu/igraph/doc/R/iterators.html

For instance to get all edges that connect to Vertex 1 you can do:

library(igraph)
x <- make_ring(10)
E(x)$color <- "black"
E(x)[adj(1)]$color <- "red"
plot(x)

enter image description here

Update:

If the vertices are not adjacent then you need to first find the path between them.

eids <- all_shortest_paths(x, 1, 4, "all")
E(x)$color <- "black"
E(x)[ eids$res[[1]] %--% eids$res[[1]] ]$color <- "red"
plot(x)