【问题标题】:igraph: Remove vertices from layoutigraph:从布局中删除顶点
【发布时间】:2016-12-07 15:09:44
【问题描述】:

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

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

【问题讨论】:

    标签: r igraph vertices


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      @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 解决方案的替代方案,通过顶点ID 的索引对locs 矩阵进行子集化:

      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")
      

      【讨论】:

        猜你喜欢
        • 2015-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        相关资源
        最近更新 更多