R

时间:2017-05-12 20:18:54

标签: r graph-visualization

我的程序根据.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 ,我希望它是黄色的。这可能吗?感谢。

1 个答案:

答案 0 :(得分:0)

一种方法:

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)

enter image description here