0

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?

Alex Fort
  • 93
  • 6
  • Please provide full [reproducible code](http://adv-r.had.co.nz/Reproducibility.html). – lukeA Mar 06 '15 at 10:06
  • Hi Luke, the code is embedded in other calculation I presented just an example, data are extracted from a database and the I reproduce about 25 different graphs in different situazion, I will try to build the code for this specific example in order to help you in assisting me. – Alex Fort Mar 06 '15 at 10:57
  • The code should also be _minimal_ - a little frame work which you can later apply to your real case. This includes necessary library calls, a dput to get the variables and so on. Makes it easier for all - and gets you answers. :-) – lukeA Mar 06 '15 at 11:00
  • 'B = matrix( c("A" ,"A", "B" ,"B","D","E", "E", "H", "H", "L", "B","D","C","F","E","G","H","M","L","N"), ncol=2) B<- data.frame(B) grf<- graph.data.frame (B, directed =TRUE, vertices=NULL) error<-array("E", "L")' – Alex Fort Mar 06 '15 at 11:12
  • I just added a messed comment to reproduce data :-) – Alex Fort Mar 06 '15 at 11:13
  • `library(igraph) B = matrix( c("A" ,"A", "B" ,"B","D","E", "E", "H", "H", "L", "B","D","C","F","E","G","H","M","L","N"), ncol=2) B<- data.frame(B) grf<- graph.data.frame (B, directed =TRUE, vertices=NULL) error<-array(c("E", "L")) V(grf)$color <- ifelse(V(grf)$name %in% error, "red", "yellow") tkplot(grf)` – Alex Fort Mar 06 '15 at 11:17
  • I am stucked in understanding how to use `edge.color` parameter in tkplot – Alex Fort Mar 06 '15 at 11:26

2 Answers2

1

Just create a color vector with the colours you want, corresponding to the edges in B:

library(igraph) 
B = matrix( c("A" ,"A", "B" ,"B","D","E", "E", "H", "H", "L", "B","D","C","F","E","G","H","M","L","N"), ncol=2) 
B<- data.frame(B) 
grf<- graph.data.frame (B, directed =TRUE, vertices=NULL) 
error<-array(c("E", "L")) 
V(grf)$color <- ifelse(V(grf)$name %in% error, "red", "yellow") 

col = rep("black", nrow(B)) 
col[B$X1 == "E" | B$X2 == "L"] <- "red" 
# or 
# col[B$X1 %in% c("E", "L") | B$X2 %in% c("E", "L")] <- "red" 
plot(grf, edge.color = col)
# or     
# tkplot(grf, edge.color = col)
lukeA
  • 53,097
  • 5
  • 97
  • 100
1

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

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • P.S. sorry I already had my code written when I noticed your sample data in the comments... Anyway, I don't think you'll have any problem in adapting my code to yours ;) – digEmAll Mar 06 '15 at 12:04