[英]How to color the vertices of a graph?
我有這樣的事情:
library(igraph)
table <- data.frame(p = c("A","B","C","D","A"), h = c("H1","H2","H3","H1","H2"))
graf_table <- graph_from_data_frame(table, directed = F)
plot(graf_table, edge.curved=0.5, vertex.size=40, edge.width=2)
我想按列為頂點着色。 這意味着 p 列的頂點是紅色的,h 列的頂點是藍色的。
這是一種為圖形的頂點着色的方法。
p
。 索引i
是一個邏輯向量,內部編碼為0/1
。i
,因為在 R 中向量索引是基於 1 的。 然后繪制圖形,記住添加前綴vertex.
到圖形參數color
。
library(igraph)
i <- names(V(graf_table)) %in% table$p
graf_table$color <- c("blue", "red")[i + 1]
plot(graf_table, edge.curved=0.5, vertex.size=40, edge.width=2,
vertex.color = graf_table$color)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.