简体   繁体   中英

Color edges in a directed network with igraph

I have a network in which I would like to highlight certain edges of node A :

  • directions going out of A should be eg colored red
  • directions goint to A should be green.
  • all other edges not connected with node A should not be printed at all.
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 .

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).

The list of edges should not be hard-coded. Only A should be given and the others should be calculated automatically.

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)))
)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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