igraph 中的颜色特定节点

Colour specific node in igraph

我正在尝试为节点 6 和 7 着色,而不管从对象 'd' 中选择了什么字母。

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 是例如绿色,其余节点为红色。

谢谢!

你可以

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

您还可以这样做:

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