簡體   English   中英

如何為圖形的頂點着色?

[英]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 列的頂點是藍色的。

這是一種為圖形的頂點着色的方法。

  1. 創建一個索引,給出頂點是否在列p 索引i是一個邏輯向量,內部編碼為0/1
  2. 將 1 添加到i ,因為在 R 中向量索引是基於 1 的。
  3. 使用該索引為頂點分配顏色。

然后繪制圖形,記住添加前綴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.

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