V(s1)[“158”] $ color< - “gold”
上面的代码只更改了一个节点的颜色。我想添加我选择的多个节点,如158,43,87,并应用相同的颜色..
如何添加节点
答案 0 :(得分:2)
这应该有效,假设158,43,87也是相应的指数
V(s1)$color[c(43,87,158)] <- "gold"
然而,如果&#34; 158&#34;,&#34; 43&#34;,&#34; 87&#34;是顶点标签,并不对应索引,那么你可以这样做
V(s1)$color[V(s1)$label %in% c("43", "87", "158")] <- "gold"
通常,您可以通过以下方式更改节点颜色:
library(igraph)
n <-sample(5:10,1)
g <- graph.ring(n)
plot(g, vertex.label=V(g)$number)
# change all node colors
V(g)$color <- "red"
# change select node colors by indices
V(g)$color[c(1,3,5)] <- "green"
plot(g, vertex.label=V(g)$number)
# change select node colors by matching node labels
V(g)$label <- paste0("v", 1:n)
V(g)$color[V(g)$label %in% c("v1", "v5")] <- "blue"
plot(g)