[R] Rgraphviz: How to control the colours of edges in a graph

Søren Højsgaard Soren.Hojsgaard at agrsci.dk
Thu Jul 6 02:21:43 CEST 2006


Using Rgraphviz, I draw the undirected graph with vertices A,B,C and D and edges A:B, B:C, C:D, D:A, A:C. I want the vertices A and B to be red and C and D to be blue. The problem is the following: I want the edges A:B and B:C to be green and the edges C:D and C:A to be yellow, while the edge A:C can have the default colour black. I assume that I have to specify this using the edgeAttrs-argument in the plot function, but I can't figure out how. Can anyone help?
Thanks.
Søren
 
Code for drawing the graph:
 
   library(Rgraphviz)
   V <- c("A","B","C","D")
   E <- list(c("A","B"),c("B","C"),c("C","D"),c("D","A"),c("A","C"))
   Eidx <- lapply(E, match, V)
   edL <- vector("list", length=length(V))
   names(edL) <- V
   for (i in 1:length(Eidx)){
     tmp <- Eidx[[i]]
     print(tmp)
     edL[[tmp[1]]]$edges <- c(edL[[tmp[1]]]$edges, tmp[2])
     edL[[tmp[2]]]$edges <- c(edL[[tmp[2]]]$edges, tmp[1])
   }
   G <- new("graphNEL", nodes=V, edgeL=edL)
   nAttrs <- list()
   nAttrs$fillcolor <- c("red","red","blue","blue")
   names(nAttrs$fillcolor) <- V 
   plot(G, "neato",nodeAttrs=nAttrs)



More information about the R-help mailing list