英文:
How to create plots in iGraph in R with matching node coordinates (and removing nodes with no edges)
问题
这里是你的翻译:
-
如你所见,绘图似乎几乎符合我的要求。然而,出于某种原因,尽管在每种情况下使用不同的节点子集(和相同的坐标),它仍然绘制所有节点。有人知道我应该如何修复这个问题吗?
-
我的第二个问题与前一个问题无关,但仍涉及相同部分的代码。您可能已经注意到绘图非常小,而且我在绘图中使用了
rescale = FALSE
。这似乎是我找到的唯一使整个图适应RStudio窗口的方法,但并不理想,而且看起来似乎不太可预测。所以如果有人知道更好的方法,那也会很感激!
希望这有助于解决你的问题。如果你需要进一步的帮助,请告诉我。
英文:
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
)
Questions:
-
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?
-
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),])
A few notes:
- I deleted most of the sizing aspects from the plotting function, for the sake of simplicity (and I will just revisit that later).
- 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.
- 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.
答案1
得分: 1
我会计算g_overall上的坐标,然后使用先前计算的坐标在子图上绘制图表(一旦删除不再在图表中的节点的坐标)。换句话说:
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),])
英文:
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),])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论