gpt4 book ai didi

r - igraph:从布局中删除顶点

转载 作者:行者123 更新时间:2023-12-04 12:49:26 31 4
gpt4 key购买 nike

我想按 4 个步骤创建图形的可视化,即不同的时间点。我的顶点(节点)的位置应该始终保持不变(使用完整图表的位置)。我只想从 R igraph 图中删除一些顶点。似乎有问题的是顶点名称发生了变化。

# Erdos
par(mfrow=c(1,3))
g <- erdos.renyi.game(20, 1/20)
locs <- layout.fruchterman.reingold(g)
V(g)$name <- V(g)
# In the original file, vector names look like this (not "1,2,3,4...):
V(g)$name <- as.vector(c(8,9,3,5,13,6,7,1,2,18,11,12,16,14,15,4,17,10,20,19))
V(g)$name

plot(g,
layout=locs,
main="Original")

# Remove a few vertices
removals1 <- c("12","2","9","11","4")
g2 <- delete.vertices(g,removals1)
plot(g2,
layout=locs[-as.numeric(removals1),],
main="Removals")

# Remove some more
removals2 <- c("15","14","7","8","5","19","10")
g3 <- delete.vertices(g2,removals2)
plot(g3,
layout=locs[-as.numeric(c(removals1,removals2)),],
main="More Removals")

如果能在这里找到解决方案,我会非常高兴。也许,还有比上面更优雅的解决方案。谢谢!

最佳答案

与其使用以某种方式移动顶点的删除(我什至无法完全覆盖图形,标签无法承受),不如使用 induced_subgraph。我不知道为什么会这样,但它似乎有效。

# Erdos
g <- erdos.renyi.game(20, 1/20)
locs <- layout.fruchterman.reingold(g)
V(g)$name <- V(g)
# In the original file, vector names look like this (not "1,2,3,4...):
V(g)$name <- as.vector(c(8,9,3,5,13,6,7,1,2,18,11,12,16,14,15,4,17,10,20,19))
V(g)$name

par(mfrow=c(1,3))
plot(g,
layout=locs,
main="Original")

# Remove a few vertices
removals1 <- c("12","2","9","11","4")
g2 <- induced_subgraph(g, V(g)[-as.numeric(removals1)])
plot(g2,
layout=locs[-as.numeric(removals1),],
main="Removals")

# Remove some more
removals2 <- c("15","14","7","8","5","19","10")
g3 <- induced_subgraph(g, V(g)[-as.numeric(c(removals1, removals2))])
plot(g3,
layout=locs[-as.numeric(c(removals1,removals2)),],
main="More Removals")

enter image description here

关于r - igraph:从布局中删除顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41020944/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com