简体   繁体   中英

color graph nodes differently

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?

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)

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM