rgraphcolorstkplot

colouring graph connections in R


I built in R a graph and I succeeded in colouring some vertex using if statement in colour, i used the tkplot function to have a better visualization.
Now I have the following graph:

FROM    TO  
A   B
A   D
B   C
B   F
D   E
E   G
E   H
H   M
H   L
L   N

and the following vertex set

E  
L

I need to plot the graph coloring the connection incoming and outcoming in E and L in RED colour while all the other in BLACK.
To be clear I need in red the following connections lines

FROM    TO
D   E
E   G
E   H
H   L
H   M

Is there a solution for this?


Solution

  • Using edge.color property isn't the only way. You can also set a color attribute to each edge, e.g. :
    (more informations about V(g) and E(g) functions can be found here)

    library(igraph)
    
    # initialize the graph
    DF <- 
    read.table(text=
    "FROM TO  
    A B
    A D
    B C
    B F
    D E
    E G
    E H
    H M
    H L
    L N",header=T,stringsAsFactors=T)
    g <- graph.data.frame(DF)
    
    # set a color (black) attribute on all edges
    E(g)$color <- 'black'
    # set red color for the edges that have an incident vertex in the set 'E,L'
    nodesSeq <- V(g)[name %in% c('E','L')]
    E(g)[inc(nodesSeq)]$color <- 'red'
    
    tkplot(g)
    

    enter image description here