简体   繁体   English

使用 igraph 在有向网络中着色边缘

[英]Color edges in a directed network with igraph

I have a network in which I would like to highlight certain edges of node A :我有一个网络,我想在其中突出显示节点A某些边缘:

  • directions going out of A should be eg colored redA出发的方向应该是红色的
  • directions goint to A should be green.A方向应该是绿色的。
  • all other edges not connected with node A should not be printed at all.根本不应该打印所有未与节点A连接的其他边。
library('igraph')

my_network <- read.table(
  header=TRUE,
  sep=",",
  text="
from,to
A,B
A,C
C,D
D,A
C,A")

set.seed(1234)
my_network_graph <- graph_from_data_frame(my_network)
plot(my_network_graph,
     edge.curved= 0.2,
)

在此处输入图像描述

So far I can highlight those edges going out of A .到目前为止,我可以突出显示A之外的那些边缘。

plot(my_network_graph,
     edge.curved= 0.2,
     edge.color = c(NA, "red")[1+ (my_network$from == "A")]
      )

在此处输入图像描述

I would like to have the edges D -> A and C -> A in blue in the same plot as with the edges A -> B and A -> C (in red).我想让边缘D -> AC -> A在蓝色的 plot 与边缘A -> BA -> C

The list of edges should not be hard-coded.边列表不应该是硬编码的。 Only A should be given and the others should be calculated automatically.只应给出A ,其他应自动计算。

You can try the code below你可以试试下面的代码

plot(my_network_graph,
  edge.curved = 0.2,
  edge.color =with(my_network,ifelse(from %in% "A","red",ifelse(from %in% c("C","D") & to == "A","blue","grey")))
)

在此处输入图像描述

or或者

plot(my_network_graph,
  edge.curved = 0.2,
  edge.color =with(my_network,ifelse(from %in% "A","red",ifelse(to == "A","green",NA)))
)

在此处输入图像描述

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

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