簡體   English   中英

R中圖形的邊緣着色

[英]Edge coloring of graph in R

我的程序基於.txt文件生成圖形。 例如,如果在文件中寫入了1,5和2,3,程序將創建連接第1和第5,第2和第3節點的圖形。 這是代碼:

library(igraph)
dat<-read.table("file.txt", header = F, sep = ",")
dat[,c(1,2)]
vertices<-as.vector(t(dat[,1:2]))
g<-graph(vertices,directed = F)
plot(g,layout=layout.circle)

我的問題是:如何根據某些條件進行邊緣着色? 例如,如果程序第一次讀取文件中的3,5 ,我希望該邊緣為紅色,然后再次讀取3,5,我希望第二個邊緣為藍色,並且如果存在第三對3,5我希望它是黃色的。 這可能嗎? 謝謝。

一種方法:

library(igraph)
df <- read.csv(text="from,to
1,2
1,2
1,3
1,2
1,3
1,2
1,2")
df$color <- with(df, ave(1:nrow(df), list(from, to), FUN=seq_along))
g <- graph_from_data_frame(df)
E(g)$color <- c("red", "blue", "yellow")[E(g)$color]
E(g)$color[is.na(E(g)$color)] <- "#CCCCCC"
plot(g)

在此處輸入圖片說明

暫無
暫無

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

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