0

My program generates a graph based on a .txt file. For example, if it is written 1,5 and 2,3 in the file, the program creates graph connecting 1st and 5th, and 2nd and 3rd nodes. Here is the code:

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)

My question is: How can I do edge coloring based on some condition? For example, if the program reads 3,5 in file for the first time, I want the edge to be red, then if it reads again 3,5 I want the second edge to be blue, and if there is a third pair of 3,5 I want it to be yellow. Is this possible? Thanks.

Alina H.
  • 21
  • 1
  • 6
  • It would be easier to help if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input. – MrFlick May 12 '17 at 20:24

1 Answers1

0

One way to do it:

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

lukeA
  • 53,097
  • 5
  • 97
  • 100