控制在R igraph中绘制节点和边的顺序?

时间:2019-01-15 19:33:43

标签: r plot igraph

是否可以控制在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)

Plot with overlap

1 个答案:

答案 0 :(得分: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)

enter image description here

此处,顶点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)

enter image description here

现在,顶点92在其他顶点之下,而顶点2实际上也很高。发生这种情况的原因是permute切换了顶点2和92:

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

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