使用igraph,当箭头指向相反方向时如何强制弯曲

时间:2013-06-01 17:53:22

标签: r plot igraph

autocurve.edges在绘制igraph图中的边缘方面做得非常出色,这样当它们指向同一方向时它们不会重叠。但是,当它们指向相反的方向时,不会施加曲率。

d <- data.frame(start=c("a","a","b","c"),end=c("b","b","c","b"))


graph <- graph.data.frame(d, directed=T)

plot(graph,
     vertex.color="white")

igraph with superimposed arrows pointing in opposite directions

问题在于b和c(或c和b)之间的箭头。

除手动指定曲率外,还有任何建议吗?

1 个答案:

答案 0 :(得分:12)

我会将edged.curved选项与autocurve.edges使用的seq调用相同。

plot(graph,
     vertex.color="white", edge.curved=seq(-0.5, 0.5, length = ecount(graph)))

enter image description here

编辑:

正如Étienne所指出的,这种解决方案还可以为独特的观察曲线绘制边缘。然后解决方案是修改autocurve.edges函数。这是我修改过的函数autocurve.edges2。基本上,它会生成一个向量,它只会弯曲非唯一边。

autocurve.edges2 <-function (graph, start = 0.5)
{
    cm <- count.multiple(graph)
    mut <-is.mutual(graph)  #are connections mutual?
    el <- apply(get.edgelist(graph, names = FALSE), 1, paste,
        collapse = ":")
    ord <- order(el)
    res <- numeric(length(ord))
    p <- 1
    while (p <= length(res)) {
        m <- cm[ord[p]]
        mut.obs <-mut[ord[p]] #are the connections mutual for this point?
        idx <- p:(p + m - 1)
        if (m == 1 & mut.obs==FALSE) { #no mutual conn = no curve
            r <- 0
        }
        else {
            r <- seq(-start, start, length = m)
        }
        res[ord[idx]] <- r
        p <- p + m
    }
    res
}

这是添加单个非相互边缘(C-> D)时的结果:

library(igraph)
d <- data.frame(start=c("a","a","b","c","c"),end=c("b","b","c","b","d"))
graph <- graph.data.frame(d, directed=T)
curves <-autocurve.edges2(graph)
plot(graph, vertex.color="white", edge.curved=curves)

enter image description here