【问题标题】:A hack to allow arrows size in R igraph to match edge width允许 R igraph 中的箭头大小与边缘宽度匹配的 hack
【发布时间】:2013-06-01 07:23:48
【问题描述】:

虽然手册指出这将是未来的功能:

arrow.size 箭头的大小。目前这是一个常数,所以它 每条边都是一样的。如果一个向量被提交,那么只有 使用第一个元素,即。如果这是从边缘属性中获取的 那么只有第一条边的属性用于所有箭头。这 未来可能会发生变化。

默认值为 1。

我想知道是否有一个技巧可以让箭头大小与边缘宽度相匹配(每条边缘都有自己的宽度)。

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


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

plot(graph,
     vertex.color="white",
     edge.width=E(graph)$size*20,
     edge.arrow.size=E(graph)$size)

【问题讨论】:

    标签: r plot igraph


    【解决方案1】:

    好吧,有一个技巧,您需要绘制与不同边宽度一样多的图形,并且每次只绘制给定的边子集,并使用“右”箭头大小。使用add=TRUE 参数将它们绘制在彼此之上。也许您还想只绘制一次顶点。

    顺便说一句。您可以通过https://github.com/igraph/igraph/issues 提交功能请求

    编辑:这是一个例子:

    library(igraph)
    
    ## (almost) your example data
    d <- data.frame(start=c("a","a","b","c"),
                    end=c("b","b","c","b"),
                    size=1:4)
    graph <- graph.data.frame(d, directed=TRUE)
    
    ## The plotting function
    eqarrowPlot <- function(graph, layout, edge.lty=rep(1, ecount(graph)),
                            edge.arrow.size=rep(1, ecount(graph)),
                            vertex.shape="circle",
                            edge.curved=autocurve.edges(graph), ...) {
      plot(graph, edge.lty=0, edge.arrow.size=0, layout=layout,
           vertex.shape="none")
      for (e in seq_len(ecount(graph))) {
        graph2 <- delete.edges(graph, E(graph)[(1:ecount(graph))[-e]])
        plot(graph2, edge.lty=edge.lty[e], edge.arrow.size=edge.arrow.size[e],
             edge.curved=edge.curved[e], layout=layout, vertex.shape="none",
             vertex.label=NA, add=TRUE, ...)
      }
      plot(graph, edge.lty=0, edge.arrow.size=0, layout=layout,
           vertex.shape=vertex.shape, add=TRUE, ...)
      invisible(NULL)
    }
    
    ## Test
    eqarrowPlot(graph, layout.auto(graph), edge.arrow.size=E(graph)$size/3,
                edge.width=E(graph)$size)
    

    不过,非常宽的边缘看起来很糟糕。

    【讨论】:

    • 我刚刚试了一下,遇到了一些问题。当我使用我给出的示例进行尝试时,它会引发错误。示例代码:d &lt;- data.frame(start=c("a","a","b","c"),end=c("b","b","c","b"), size=rnorm(4)) for(i in 1:nrow(d)){ graph &lt;- graph.data.frame(d[1,], directed=T) plot.new() plot(graph, vertex.color="white", edge.width=E(graph)$size*20, edge.arrow.size=E(graph)$size, add=T) } 如果我尝试使用自己的数据(其中还包含顶点的坐标,每个循环中绘制的顶点之一。有没有办法绘制所有顶点,然后一次选择每个边?
    • @EtienneLow-Décarie:添加了一个示例。
    • @GaborCsardi 美丽。我认为 iGraph 在这方面并不是特别灵活。
    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    相关资源
    最近更新 更多