如何为图中的组中的节点着色
How to colour nodes in groups in graph
V(s1)["158"]$color <- "gold"
以上代码只改变了一个节点的颜色。我想添加我选择的多个节点,比如 158,43, 87 并应用相同的颜色..
如何添加节点
这应该有效,假设 158、43、87 也是相应的索引
V(s1)$color[c(43,87,158)] <- "gold"
但是,如果“158”、“43”、“87”是顶点标签并且不对应于索引,那么您可以改为这样做
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)
V(s1)["158"]$color <- "gold"
以上代码只改变了一个节点的颜色。我想添加我选择的多个节点,比如 158,43, 87 并应用相同的颜色..
如何添加节点
这应该有效,假设 158、43、87 也是相应的索引
V(s1)$color[c(43,87,158)] <- "gold"
但是,如果“158”、“43”、“87”是顶点标签并且不对应于索引,那么您可以改为这样做
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)