如何避免 igraph 中的双箭头

How to avoid the double arrows in igraph

用下面的代码

library(igraph)

actors <- data.frame(name=c("a","b","c","d","e","f","g","h"))
relations <- data.frame(from=c("a", "a", "b", "b",
                               "b", "c", "d","d",
                               "g","e","g","d"),
                        to  =c("d", "e", "c", "e", 
                               "g", "f", "f", "g",
                               "h","b","d","a"),
                        weight = c(14,30,25,3,5,6,4,13,2,6,10,10))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
test.layout <- layout_(g,with_dh(weight.edge.lengths = edge_density(g)/1000))
plot(g,vertex.size=30,edge.arrow.size= 0.5,edge.label = relations$weight,
     layout = test.layout)

我生成加权有向图 我想避免某些边缘末端的双箭头。相反,我想看到两条不同的边(例如从 d 到 a 和从 a 到 d)。

您在生成布局之前没有设置随机种子,所以我无法准确了解您的布局。尽管如此,您可以通过使用 edge.curved 参数 igraph.plot.

来获得两条单独的边
ENDS = ends(g, E(g))
Curv = rep(F, nrow(ENDS))
for(i in 1:nrow(ENDS)) {
    Curv[i] = are.connected(g, ENDS[i,2], ENDS[i,1]) }

plot(g,vertex.size=30,edge.arrow.size=0.5,edge.label = relations$weight,
     layout = test.layout, edge.curved=Curv)

如果你不介意所有弯曲的边缘,你可以直接使用edge.curved=TRUE(但你可以自定义你的情节如你所愿, 的回答看起来更好)

plot(g,
  vertex.size = 30, edge.arrow.size = 0.5, edge.label = relations$weight,
  layout = test.layout,
  edge.curved = TRUE
)