J.Q
J.Q

Reputation: 1031

coloring nodes in iGraph

HAVE is a data frame with musical Artists, their Friends, and the Genre of the Artists:

Artist         Friend       ArtistGenre
2 Chainz       Boy Wonder   HIPHOP
2 Chainz       Chris Brown  HIPHOP
2 Chainz       Drake        HIPHOP
Billy Joel     Cindi Lauper ROCK
Blake Shelton  Gwen Stefani COUNTRY 

NEED is a graph showing connections between Artist and Friend, where all people in Artist have a node color varying by ArtistGenre.

I produce a graph without conditionally colored nodes here:

x <- graph_from_edgelist(as.matrix(HAVE[,1:2]), directed = F) plot.igraph(x, vertex.label=NA, vertex.size=1.5)

But changing the node colors by Genre is proving quite difficult. How do I map ArtistGenre to node colors?

Upvotes: 2

Views: 303

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

Here is one way of doing it. This will colour the Artist nodes by genre, keeping the Friend nodes as white. I have added labels and made the nodes bigger so that you can see what is happening...

plot.igraph(x, 
            vertex.label = vertex.attributes(x)$name, 
            vertex.size = 25,
            vertex.color = match(vertex.attributes(x)$name,
                                 HAVE$Artist))

enter image description here

Upvotes: 4

Related Questions