0

I need to color nodes differently to plot graph communities (set of nodes) in R. For this case, I deal with 17 communities ( so I need 17 different color). to color nodes I use this command.

 V(g5)$color<- ifelse(V(g5)$name %in% V(g3)$name,com$membership+1, "white")

com$membership    
1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17  1  1  1  1  1  1  1  1  1  1  1  1  2  2  2  2  2  2  2  2  2  2  2  2  3  3  3  3  3  3  3  3  3  4  4  4 4  4  4  4  4  4  5  5  6  6  6  6  6  6  6  6  7  7  8  8  8  8  9  9  9  9  9  9  9  9 10 10 10 10 10 10 10 11 11 11 11 11 11 12 12 13 13 13 13 14 14 14 14 15 15 15 15 16 17 17 9 14

and to plot :

 plot(g5, vertex.color=V(g5)$name)

the problem that i get only 6 color that it repeat to the other communities. How can i correctly color this 17 communities differently?

989
  • 12,579
  • 5
  • 31
  • 53
Sasa88
  • 327
  • 1
  • 3
  • 15
  • You should post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It's unclear how `g5` and `g3` and `com` relate to each other. – MrFlick Apr 14 '15 at 14:45
  • 'g5' and 'g3' are two graphs, and 'com' is the community structure i.e. it's means to which community a node belongs. – Sasa88 Apr 14 '15 at 16:26

1 Answers1

1

If you just specify color with a numerical index, R will pull the colors from the current palette(). By default this contains 8 different colors.

palette()
# [1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" 
# [8] "gray"

If you specify an index greater than 8, R will just loop around the index such that 1==9.

You can change the pallette to contain more colors

palette(rainbow(17))

Or you could explicitly set the colors rather than specifying a color index.

mycols <- rainbow(17)
V(g5)$color<- ifelse(V(g5)$name %in% V(g3)$name,mycols[com$membership], "white")

This is probably "safer" than changeing the palette since that will affect all other plots as well.

g <- graph.ring(17)
V(g)$color <- rainbow(17)
plot(g)

enter image description here

Note: It's not that easy to find 17 different colors that you can easily distinguish by eye.

MrFlick
  • 195,160
  • 17
  • 277
  • 295