简体   繁体   English

有没有办法在 igraph R 中为输入和输出单独加权边缘?

[英]Is there a way to have separate weighted edges for ins and outs in igraph R?

I'm making a directed network in R studio with the igraph package.我正在使用 igraph package 在 R 工作室制作定向网络。 I would like to display two edges between nodes where applicable, both weighted, one for ins and one for outs.我想在适用的情况下显示节点之间的两条边,都是加权的,一条用于输入,一条用于输出。 I'm very new to R and managed to get weighted edges that compile both ins and outs but would like them separated.我对 R 非常陌生,并设法获得了可以编译输入和输出但希望它们分开的加权边缘。

I've googled my problem with no avail, it might be from phrasing it wrong.我用谷歌搜索我的问题无济于事,可能是措辞错误。 I apologize in advance if I worded it badly.如果我措辞不好,我会提前道歉。

EDIT: Minimal reproducible sample:编辑:最小的可重复样本:

OPR.df <- data.frame("From" = c(c("8", "8", "8", "8", "7", "25", "24", "1A", "12", "12"),
                                c("12", "12", "12", "17", "17", "17"),
                                c("17", "17", "17", "17"),
                                c("17", "17", "17", "17", "17", "9A", "9", "17", "9", "17", "9"),
                                c("9", "17", "17", "17")),
                     "To" = c(c("8", "8", "8", "7", "25", "24", "1A", "12", "12", "12"), 
                              c("12", "12", "17", "17", "17", "17"),
                              c("17", "17", "17", "17"),
                              c("17", "17", "17", "17", "9A", "9", "17", "9", "17", "9", "17"),
                              c("17", "17", "17", "17")))


opr.d <- graph_from_data_frame(d = OPR.df,  
                             directed = T)

# I think this is the part where I set this??
E(opr.d)$weight <- 1

opr.sd <- simplify(opr.d,
            remove.multiple = T,
            remove.loops = F,
            edge.attr.comb = c(weight = "sum",
                               type = "ignore"))
E(opr.sd)$width <- E(opr.sd)$weight/3

There are a number of things that you can do to make the two-way links more visible.您可以做很多事情来使双向链接更加明显。 First, plotting using the default layout crowds the vertices 9, 9A and 17 too close together.首先,使用默认布局进行绘图会使顶点 9、9A 和 17 过于靠近。 There is no room to see the edges.没有空间可以看到边缘。 I will use layout_with_graphopt , which works fine for this example, although for more complex examples you may need to tune up the layout even more.我将使用layout_with_graphopt ,它适用于本示例,但对于更复杂的示例,您可能需要进一步调整布局。

set.seed(4321)
plot(opr.sd, xpd=NA)
set.seed(4321)
plot(opr.sd, layout=layout_with_graphopt)

使用默认布局和 graphopt 绘图

Of course, we still have the problem from your original question: the arrows overlap each other.当然,您的原始问题仍然存在问题:箭头相互重叠。 You can fix this using the edge.curved argument.您可以使用edge.curved参数解决此问题。 I wanted all of the arrows to be straight except where they overlap, so I created a customized curvature vector to adjust only the overlapping edges.我希望所有的箭头都是直的,除了它们重叠的地方,所以我创建了一个自定义的曲率向量来只调整重叠的边缘。 Also, the arrow heads are too big and made it hard to see the arrows, so I made the heads a bit smaller.另外,箭头太大,很难看到箭头,所以我把箭头做得小了一点。 All together, we get:总之,我们得到:

CURV = rep(0,ecount(opr.sd))
CURV[2]  = 0.6
CURV[11] = 0.6
CURV[13] = 0.6

set.seed(4321)
plot(opr.sd, layout=layout_with_graphopt, 
    edge.arrow.size=0.7, edge.curved=CURV, frame=T)

带有一些弯曲边的图

You might still want to tweak this a bit, but I think this shows the path to solving your problem.您可能仍想稍微调整一下,但我认为这显示了解决问题的途径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM