4

无论从对象“d”中选择什么字母,我都试图为节点 6 和 7 着色。

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 是绿色的,其他节点是红色的。

谢谢!

4

2 回答 2

6

你可以做

V(g)$color <- "red" 
V(g)$color[6] <- "white"
V(g)$color[7] <- "green"
于 2015-08-13T15:52:07.020 回答
4

你也可以这样做:

V(g)["nameofnode"]$color<-"red"
于 2017-03-07T12:15:13.900 回答