R: Selectively display nodes when plotting an igraph

风格不统一 提交于 2020-01-04 02:03:07

问题


How can I plot a selection of igraph nodes?

I have an existing graph, but it is too complex. I want to be able to 'zoom in' on a subset of the nodes.

I am able to delete a subset of edges, but I can't figure out how to 'turn off' the isolated nodes.

When using the network package, the displayisolates=FALSE parameter does this; it does not display these isolated nodes.

The layout algorithm should also ignore the 'turned off' edges.

For example:

g1 <- graph( c( 0,1, 1,2, 2,2, 2,3 ) )
g2 <- delete.edges(g1, E(g1, c(0,1)))
plot(g2)

When plotting g2, I want to not display node 0.

Thanks


回答1:


I understand that users should not submit new answers to comment on other answers, but my edit was rejected and I don't have a high enough reputation to leave comments.

I just wanted to point out that in Wine's answer above, the "- 1" index correction in the deletes.isolates function is not necessary from igraph 0.6 onwards. See also Tamas' comment here:

Plot only Edges with a specific weight - igraph




回答2:


Hey, it looks like you figured it out, but in exploring the question (I usually use the network package myself, but have tried to use igraph as well for some things) I came up with a function that should do that automatically, mirroring the displayisolates = F functinality.

delete.isolates <- function(graph, mode = 'all') {
  isolates <- which(degree(graph, mode = mode) == 0) - 1
  delete.vertices(graph, isolates)
}

In your case, running this with g1 would remove the first vertex if you used the argument mode = 'in' and the last vertex if you used the argument mode = 'out'.

So in your case, if you entered:

g2 <- delete.isolates(g1, mode = 'in')
plot(g2)

You should get what you want. I don't use igraph much, so it's very possible that this function would run into some issues for other graphs.

P.S. This also gives the kind of weird result that in the new g2, the first vertex is now an isolate based on indegree. This function probably isn't useful in most situations, but might be helpful for making a cleaner plot.




回答3:


iso <- V(g1)[degree(g1)==0]
g2 <- delete.vertices(g1, iso)


来源:https://stackoverflow.com/questions/5643002/r-selectively-display-nodes-when-plotting-an-igraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!