igraph:从布局

时间:2016-12-07 15:09:45

标签: r igraph vertices

我想以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")

我很高兴在这里找到解决方案。也许,如上所述,还有更优雅的解决方案。谢谢!

2 个答案:

答案 0 :(得分:3)

不使用以某种方式移动顶点的删除(我甚至无法完全覆盖图形,标签不能承受),最好使用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

答案 1 :(得分:1)

@ emiliman5的回答是现货;但是,我想展示一个稍微不同的解决方案和原始问题。

locs[-as.numeric(removals1),]行未删除顶点 c("12","2","9","11","4"),而是 c("12","2","9","11","4")

比较

> head(cbind(as_ids(V(g)),locs))
     [,1] [,2]               [,3]              
[1,] "8"  "42.1520624498397" "29.0822309512088"
[2,] "9"  "42.9864581422991" "28.6882159221222"
[3,] "3"  "42.9653898313169" "30.9232356041607"
[4,] "5"  "46.6704380162041" "29.7404624492056"
[5,] "13" "47.4190242396939" "28.5469829852443"
[6,] "6"  "46.6173689817953" "25.6916967155951"

致:

> head(cbind(as_ids(V(g2)),locs[-as.numeric(removals1),]))
     [,1] [,2]               [,3]              
[1,] "8"  "42.1520624498397" "29.0822309512088"
[2,] "3"  "42.9653898313169" "30.9232356041607"
[3,] "5"  "47.4190242396939" "28.5469829852443"
[4,] "13" "46.6173689817953" "25.6916967155951"
[5,] "6"  "44.3293887668239" "30.6957434444784"
[6,] "7"  "47.4947062707832" "27.0391131188028"

请注意,节点5的坐标不同。在子集化后检查x坐标和y坐标是否匹配:

> r1 <- cbind(as_ids(V(g)),locs)
> r2 <-cbind(as_ids(V(g2)),locs[-as.numeric(removals1),])
> r1[match(r2[,1],r1[,1]),2] == r2[,2]
 [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
> r1[match(r2[,1],r1[,1]),3] == r2[,3]
 [1]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE

作为induced_subgraph解决方案的替代方案,将locs矩阵按顶点id的索引进行子集化:

plot(g2, 
     layout=(locs[-which(as_ids(V(g)) %in% removals1),]), 
     main="Removals")

which(as_ids(V(g)) %in% removals1)获取指定顶点的行索引:

> which(as_ids(V(g)) %in% removals1)
[1]  2  9 11 12 16

顶点“9”在第2行,顶点“2”在第9行,等等

对于第三张图:

removals2 <- c("15","14","7","8","5","19","10")
g3 <- delete.vertices(g2,removals2)
plot(g3, 
     layout=locs[-locs[-which(as_ids(V(g)) %in% c(removals1, removals2)),], 
     main="More Removals")