Reputation: 93
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?
Upvotes: 0
Views: 112
Reputation: 57210
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)
Upvotes: 1
Reputation: 54237
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)
Upvotes: 1