Sabor117
Sabor117

Reputation: 135

How to create plots in iGraph in R with matching node coordinates (and removing nodes with no edges)

So, this question is actually something of a two-parter and I think has something of a complex background, but I really hope that the solution/s won't be that tricky.

Essentially I am plotting biological pathways in R using iGraph, my intention is to take a subset of genes (nodes) from each pathway and plot them, then take a second (and third/fourth) subset of genes and plot those as well. I'm plotting the networks as trees for ease of viewing and I also, specifically, want each pathway subset to have the same nodes in the same positions (some subsets only differ in 1/2 nodes, some differ in dozens). Again, this is for ease of viewing (instead of having names), so for example I would just like GENE1 and GENE2 to be in the same position in relation to each other each time, but obviously in some plots GENE1 or GENE2 will not be present.

Here is my current code (along with a few comments) and some example plots uploaded:

coords = layout_as_tree(isubpathway_info, ### isubpathway_info is the iGraph object
                          root = match(geneKEGG, V(isubpathway_info)$name), ### Matching the nodes to names
                          circular = FALSE,
                          flip.y = FALSE,
                          mode = "all")

Edit for clarity: The above and below are the two relevant parts of the code which occur within a loop. The main thing changing with each loop is that it loads in a different graph object and only uses the first instance of the loop to create the coordinates. I haven't included the rest of the code because it didn't really seem to be relevant to the question.

### This is done as a loop, so the isubpathway_info is different each time but coords should be the same
### I.e.: different nodes/edges but same coordinates

plot(isubpathway_info,
       layout = coords,
       vertex.size = 50,
       #vertex.label = gene_details$hgnc_symbol[gene_nums], ### I'm not currently using this because labels basically obscure everything
       vertex.label = NA,
       rescale = FALSE,
       ylim = c(1,4),
       xlim = c(-10,10),
       asp = 0
       )

Two example plots from different pathway subsets

Questions:

  1. So, as you can see the plotting seems to be almost doing what I want. However, for some reason it is still plotting all of the nodes, despite the fact that it's using different node subsets (and the same coordinates) in each case. Does anyone know how I should fix this?

  2. My second question isn't related to the former but is still involving the same segment of code. You might have noticed that the plots are pretty small, and also that I have rescale = FALSE in the plot. This seemed to be the only way I could find to make the entire plot actually fit in the RStudio window, but it's not ideal and seems sort of unpredictable as to what size it will actually be. So if anyone knows of a better way of doing that, it would also be appreciated!

Edit 2:

At the request of one of the commenters I have tried to create a reproducible example of my problem. This led me to another question on here which seemed relatively similar to my own, however, the solution they used did not seem to work for me.

set.seed(123)

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

removals1 = c("2" ,"5" ,"13", "19", "25")
removals2 = c("2" ,"5" ,"11", "13", "19", "22", "24", "25")

g1 = induced_subgraph(g_overall, V(g_overall)[-as.numeric(removals1)])
g2 = induced_subgraph(g_overall, V(g_overall)[-as.numeric(removals2)])

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

plot.igraph(g1,
        layout = coords)

plot.igraph(g2,
        layout = coords[-as.numeric(removals),])

Reproducible graphing attempt

A few notes:

  1. I deleted most of the sizing aspects from the plotting function, for the sake of simplicity (and I will just revisit that later).
  2. In theory the sub-pathways I will be comparing will all come from one larger pathway, hence why I created both graph objects from one larger graph object (this will actually be done by a different method, but I'm hoping this is similar enough). You also might comment I haven't taken into account any situation where g2 has a node that g1 doesn't, but I'm hoping this won't be an issue for me.
  3. You might note as well that, for whatever reason, this removal method I am using has NOT actually correctly removed the intended vertices (for example neither side should have a "5" vertice and both do). That alone is a pain, but isn't exactly what I'm concerned with. What bothers me more is that the numbers should, in theory, be in the same positions in each graph when they are present. But they aren't...

I really do hope though that this allows someone to come to my rescue here!

P.s. In this instance g1 and g2 would both be an isubpathway_info in different iterations of the loop.

Upvotes: 0

Views: 967

Answers (1)

glagla
glagla

Reputation: 611

I would compute the coordinates on g_overall and then make the plots on the subgraphs with the coordinates previously calculated (once the coordinates of the nodes that are no longer in the graphs are removed). In other words:

set.seed(123)

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

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

removals1 = c("2" ,"5" ,"13", "19", "25")
removals2 = c("2" ,"5" ,"11", "13", "19", "22", "24", "25")

g1 = delete_vertices(g_overall, removals1)
g2 = delete_vertices(g_overall, removals2)

plot.igraph(g1,
        layout = coords[-as.numeric(removals),])

plot.igraph(g2,
        layout = coords[-as.numeric(removals),])

Upvotes: 1

Related Questions