向`R`中的`igraph`添加边

问题描述 投票:0回答:1

有一些类似的帖子(例如这个这个),但我的问题可能对于

igraph
包功能来说更基本。

这是生成简单图表的最小示例:

library(MASS)
library(pcalg)
library(igraph)

# Some variance-covariance
Corrs <- matrix(c(1.0,0.6,0.7,0.5,0.6,1.0,0.5,0.6,0.7,0.5,1.0,0.7,0.5,0.6,0.7,1.0), 4, 4)
SDs <- c(1.0,0.5,2.0,1.0)
Covs <- SDs %*% t(SDs) * Corrs

dat = as.data.frame(mvrnorm(100, mu=c(0,0,0,0), Sigma=Covs))

n = nrow(dat)
V = colnames(dat) # node names
pc1 = pc(suffStat=list(C=cor(dat), n=n),
    indepTest=gaussCItest, # indep.test: partial correlations
    alpha=0.05, labels=V, u2pd='retry')

gr = graph_from_graphnel(pc1@graph)

plot.igraph(gr, layout=layout_in_circle,
    vertex.label=V, vertex.shape='circle', vertex.size=30,
    vertex.label.cex=0.55, vertex.label.color='black',
    edge.arrow.size=0.6)

如果我尝试添加一条边(例如,V2 —> V3),它仅以该边结束,而其他边则“原始”边消失了:

gr = graph_from_graphnel(pc1@graph) + edge(2, 3, color='blue')

plot.igraph(gr, layout=layout_in_circle,
    vertex.label=V, vertex.shape='circle', vertex.size=30,
    vertex.label.cex=0.55, vertex.label.color='black',
    edge.arrow.size=0.6)

我也尝试使用

add_edges()
函数并得到相同的结果。

那么,我怎样才能在现有图表中添加一条边呢?我怎样才能用不同的

color
(我管理的)和
lty
来标记特定的边缘?

r igraph
1个回答
1
投票
g <- gr |>
  set_edge_attr("color", value = "green") |>
  add_edges(c(2,3), color = "blue")

plot.igraph(g, layout=layout_in_circle,
            vertex.label=V, vertex.shape='circle', vertex.size=30,
            vertex.label.cex=0.55, vertex.label.color='black',
            edge.arrow.size=0.6)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.