简体   繁体   中英

Removing or hiding specific edges in iGraph based on nodes

This question is something of a follow-on from a previous question of mine and I have been wrestling with for a while.

I am attempting to visualise networks in R using the iGraph package, however, I want to visualise different subsets of the same network in the exact same layout. So there will be vertices which differ between the subsets, but I want each vertex and edge to be in the same place in each plot.

Initially I tried to do this by creating the coordinates of an overall graph and then plotting subgraphs with the same coordinates. However, this ended up having several issues:

  1. iGraph bases vertex positions on their number, so as soon as you remove one vertex from a graph for a subgraph, the numbers of all subsequent vertices change and therefore their positions will be wrong.
  2. By giving the coordinates of vertices which no longer exist, iGraph (I think) still tries to plot them so you can end up with nodes unconnected to the subgraph.

So, instead I tried a different tactic. Now I plot the overall graph but try to simply colour our the parts of the graph I do no want. So, in the following example:

set.seed(123)

g_overall = erdos.renyi.game(25, 0.3)

removals = c("2" ,"5" ,"13", "19", "25")

coords = layout_as_tree(g_overall,
               root = 1,
               circular = FALSE,
               flip.y = FALSE,
               mode = "all"
               )

V(g_overall)$colour = ifelse(V(g_overall) %in% removals, "blue", "red")

plot.igraph(g_overall,
      layout = coords,
      vertex.shape = "none",
      vertex.label = V(g_overall),
      vertex.label.color = V(g_overall)$colour
      )

This gives the following:

基本情节

Then if I change the code to:

plot.igraph(g_overall,
      layout = coords,
      vertex.shape = "none",
      vertex.label = ifelse(V(g_overall)$colour == "red", V(g_overall), NA),
      vertex.label.color = V(g_overall)$colour
      )

This becomes:

从情节中删除的项目

This is ALMOST right now, however obviously there are now edges which need to be removed and I don't know how to approach that. Ideally I would use a similar ifelse loop with something like: ifelse(starting_node$colour == "red", edge_colour = "black", edge_colour = "white" so that the edge colour is based on the nodes its connected to (and goes invisible like the nodes). But I cannot think of a way to do this.

Any advice appreciated.

Ps I will, of course, look into answers which are based around the complete removal of edges, but I suspect that this will change the layout and so would rather stick to "hiding" them.

I think that you can get what you want more simply by removing the unwanted nodes rather than hiding them. Before I do that, I want to make a small change to your code, namely:

removals = c(2, 5, 13, 19, 25)

With that,

g2 = induced_subgraph(g_overall, V(g_overall)[-removals])
coords2 = coords[-removals,]

plot.igraph(g2,
      layout = coords2,
      vertex.shape = "none",
      vertex.label = V(g_overall)[-removals],
      vertex.label.color = V(g_overall)$colour[-removals]
      )

简化图

I kept the same labels on the reduced graph mimicking your code. For another way to reduce the graph, but keep the old labels see nodes labels after deleting in R

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM