簡體   English   中英

如何為圖中的組中的節點着色

[英]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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM