简体   繁体   English

颜色图节点不同

[英]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). 我需要对节点进行不同的着色以在R中绘制图社区(节点集)。在这种情况下,我处理了17个社区(因此我需要17种不同的颜色)。 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. 我只得到6种颜色的问题,重复到其他社区。 How can i correctly color this 17 communities differently? 我该如何正确地为这17个社区涂上不同的颜色?

If you just specify color with a numerical index, R will pull the colors from the current palette() . 如果仅使用数字索引指定颜色,则R将从当前的palette()提取颜色。 By default this contains 8 different colors. 默认情况下,它包含8种不同的颜色。

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 . 如果您指定的索引大于8,则R只会在索引周围循环,以使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. 注意:要找到可以用肉眼轻松区分的17种不同的颜色并非易事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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