【问题标题】:Control the order in which nodes and edges are drawn in R igraph?控制 R igraph 中节点和边的绘制顺序?
【发布时间】:2019-01-15 19:33:42
【问题描述】:

是否可以控制在igraph 图中绘制节点和边的顺序?类似于ggplot2 在数据框中按照它们在arranged 的顺序绘制点的方式。我知道绘制的节点和边缘肯定会重叠,但我希望能够控制哪些最明显(即绘制在顶部的那些)。我有一个下面有一些重叠的图表。

library(igraph)
library(scales)
col_fun <- colorRampPalette(c('tomato', 'skyblue'))

g <- erdos.renyi.game(100, .025)

V(g)$label <- NA
V(g)$size <- scales::rescale(degree(g), c(5,15))

V(g)$color <- col_fun(vcount(g))

E(g)$color <- col_fun(ecount(g))

plot(g)

【问题讨论】:

    标签: r plot igraph


    【解决方案1】:

    就像在ggplot2 中我们查看数据框中的行号一样,在igraph 中我们也查看顶点ID。举个例子,让

    set.seed(1)
    g <- erdos.renyi.game(100, .05)
    V(g)$name <- 1:100
    V(g)$size <- scales::rescale(degree(g), c(3, 20))
    V(g)$color <- col_fun(vcount(g))
    V(g)$color[92] <- "#FF0000"
    V(g)$color[2] <- "#00FF00"
    plot(g)
    

    这里的顶点 2 是小而绿色的,而顶点 92 是大而红色的。请注意,顶点已命名。还可以看到具有较高数字的顶点位于具有较低数字的顶点之上(顶点名称也对应于它们的顺序)。另一方面,

    set.seed(1)
    g <- erdos.renyi.game(100, .05)
    V(g)$name <- 1:100
    idx <- 1:100
    idx[c(92, 2)] <- c(2, 92)
    g <- permute(g, idx)
    V(g)$size <- scales::rescale(degree(g), c(3, 20))
    V(g)$color <- col_fun(vcount(g))
    V(g)$color[2] <- "#FF0000"
    V(g)$color[92] <- "#00FF00"
    plot(g)
    

    现在顶点 92 低于其他顶点,顶点 2 实际上也相当高。这是因为permute 切换了顶点 2 和 92:

    idx <- 1:100
    idx[c(92, 2)] <- c(2, 92)
    g <- permute(g, idx)
    

    这不是特别方便,但我不知道有任何其他方法可以重新排序顶点。

    【讨论】:

      猜你喜欢
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多