2015-08-13 150 views
2

我試圖爲節點6和7着色,而不管從對象'd'中選擇的任何字母。igraph中的顏色特定節點

g <- graph_from_literal(1:2:3:4:5 -- 6:7) 
# Rename (sum up all the vertices) 
d <- c("a", "b", "c", "d", "e", "f", "g","h", "i", "j") 

V(g)$name <- sample(d, 7, replace=TRUE) 
colours <- c("red") 
V(g)$color <- ifelse(V(g)$name == c('a','e'), "white", colours) 

plot.igraph(g, layout=layout_with_dh, vertex.label=V(g)$name, 
vertex.size=35, 
vertex.color=V(g)$color, #colors.r 
vertex.label.cex=0.7, 
) 

我試過了上面的ifelse(),但它們似乎沒有取數值。我希望得到一些幫助。

我想要的是節點6是例如白色和7是例如綠色,其他節點是紅色的。

謝謝!

回答

5

你可以做

V(g)$color <- "red" 
V(g)$color[6] <- "white" 
V(g)$color[7] <- "green" 
+0

謝謝你這麼多。 – Sam

1

你也可以這樣做:

V(g)["nameofnode"]$color<-"red"