Choose color ggraph R
Asked Answered
L

1

1

I try to plot a bipartite network with the edges of the same color than one of their nodes.
For example, let's take an movie/actor bipartite graph as an exemple, with 7 movies and 15 actors, and each actor has a nationality.
I want to have the edge between an actor and a movie of the same color than the nationality of the actor.

NG1 <- 7
NG2 <- 15
Nat <- sample(x = c("French", "English", "Italian", "American", "Chinese"), size = NG2, replace = T)
G <- graph.empty(NG1+NG2)

[Here, head(Nat) returns "Italian" "English" "American" "French" "French" "French"]
The code to create the edgelist:

E1 <- sample(x=1:NG1, size = 30, replace = T)
E2 <- sample(x=(NG1+1):(NG1+NG2), size = 30, replace = T)
EL <- c(rbind(E1, E2))
G <- add_edges(G, EL, nat = Nat[E2-NG1])

[Here, head(EL) returns 1 14 3 13 2 15]
The different aes arguments:

GROUP <- c(rep("Movie", NG1), rep("Act", NG2))
COL <- c(rep("Movie", NG1), Nat)
TXT <- c(as.character(1:NG1), letters[1:NG2])

And now the ggraph instructions:

 ggraph(G, layout = 'kk') + 
      geom_node_point(aes(col = COL, shape = GROUP, size = 7)) + 
      geom_edge_link(aes(col = nat)) +
      geom_node_text(aes(label = TXT), size = 4)

Plot

As you can see in the bottom, actor a, which is Italien has a blue node, but is connected with a pink edge with movie 7... How can I specify the color palette for nodes (here 6 colors) and edges (the 5 first colors of the nodes)?

I hope that I have made clear.

Leitmotif answered 26/5, 2017 at 14:1 Comment(0)
L
3

After two hours, I finally found a solution !

I used the function gg_color_hue defined here to emulate the 6 colors used for the nodes and then:

+ scale_edge_colour_manual(values = cols[1:5])
Leitmotif answered 26/5, 2017 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.