【问题标题】:Multi-panel network figure using a loop?多面板网络图使用循环?
【发布时间】:2021-08-19 17:35:59
【问题描述】:

我正在尝试使用 igraph 包中的网络制作多面板图。我想要 2 行,每行有 3 个网络。我需要能够将图形保存为 PNG,并且我想在其中一个角落将它们标记为 A:F。我尝试循环执行此操作,但图中仅显示一个网络。我需要循环中的V(nw)$x<- yE(nw)$x<- y 代码才能使我的网络正常运行。我的网络在 list() 中。

我已经制作了我尝试过的代码的一小部分示例,如果可以的话,我想避免在没有循环的情况下这样做。提前致谢。

srs_1nw <- graph("Zachary")
srs_2nw <- graph("Heawood")
srs_3nw <- graph("Folkman")

srs_1c <- cluster_fast_greedy(srs_1nw)
srs_2c <- cluster_fast_greedy(srs_2nw)
srs_3c <- cluster_fast_greedy(srs_3nw) 

listofsrs_nws <- list(srs_1nw,srs_2nw,srs_3nw)
listofsrs_cs <- list(srs_1c,srs_2c,srs_3c)
  

colours <- c("red","blue","green","yellow")

par(mfrow=c(2,3))
for (i in length(listofsrs_nws)) {
  c<-listofsrs_cs[[i]]
  nw<-listofsrs_nws[[i]]
  
  V(nw)$size <- log(strength(nw))*6       # weighted nodes
  E(nw)$arrow.size <- 2                   # arrow size
  
  c.colours <- colours[membership(c)]
  
  plot(c, nw, col = c.colours,
     mark.col = adjustcolor(colours, alpha.f = 0.4),
     mark.border = adjustcolor(colours, alpha.f = 1),
     vertex.frame.width = 5, edge.curved = .15)
}

【问题讨论】:

  • length(listofsrs_nws) 应该是 seq_along(listofsrs_nws) 。然后绘制剩余的面板。
  • 谢谢!我会试试的。

标签: r loops igraph figure


【解决方案1】:

我们可以像下面这样使用mapply

mapply(function(c, nw) {
  V(nw)$size <- log(strength(nw)) * 6 # weighted nodes
  E(nw)$arrow.size <- 2 # arrow size
  c.colours <- colours[membership(c)]
  plot(c, nw,
    col = c.colours,
    mark.col = adjustcolor(colours, alpha.f = 0.4),
    mark.border = adjustcolor(colours, alpha.f = 1),
    vertex.frame.width = 5, edge.curved = .15
  )
}, listofsrs_cs, listofsrs_nws)

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多