Reputation: 31
Very simply, I just want to represent a multiplexed graph and color the links according to their type (variable of an attribute of the links attribute table).
I read several topics on the forum where there are two possible types of links (so two possible colors) but in my case I have 7 possible.
I guess it's a basic syntax question but I can't answer it alone as I'm a beginner. I hope someone can help me
I tried two methods but they don't work.
The first one works but not what I wantonly : allows to query one type of link: only for "cohab" are colored. Now I want the "cohab" to be in orange, the "copol" in red, the "C" in green, etc. and if missing values, in grey.
plot(net, edge.color= c("grey", "orange")[(E(net)$type=="cohab")+1],vertex.size=15,
vertex.color="gray40",layout=layout_in_circle, edge.curved=.3)
colrs <- c("gray50", "tomato", "gold", "orange","blue","black","green")
E(net)$color <- colrs[E(net)$type]
plot(net, edge.color=colrs,vertex.size=15,
vertex.color="gray40",layout=layout_in_circle, edge.curved=.3)
Upvotes: 0
Views: 267
Reputation: 174506
You can convert the edge attribute to a factor, then numeric, then use this as the index for the colrs
vector
colrs <- c("gray50", "tomato", "gold", "orange","blue","black","green")
plot(net, edge.color = colrs[as.numeric(factor(E(net)$type))],
vertex.size = 15,
vertex.color="gray40", layout = layout_in_circle, edge.curved=.3)
legend(0.95, -0.5, legend = levels(factor(E(net)$type)), col = colrs, lwd = 1)
Data used
library(igraph)
set.seed(1)
df <- expand.grid(from = LETTERS[1:22], to = LETTERS[1:22])
net <- graph_from_data_frame(df[df$from != df$to,], directed = FALSE)
edge.attributes(net) <- list(type = sample(c('cohab', 'copol', 'C', 'D',
'small', 'large', 'crazy'),
gsize(net), TRUE))
Upvotes: 3