NA_
NA_

Reputation: 31

Igraph - Multiplex networks - How to colour edges by edges's attributes (more than 2 different colors)?

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.method 1 where only one type of link is colored

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)

The second one doesn't works. inaccurate and incomplete visualization (method 2)

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

Answers (1)

Allan Cameron
Allan Cameron

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)

enter image description here


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

Related Questions