Søren Højsgaard
2005-Jul-31 11:46 UTC
[R] Drawing a graph with vertices and edges using tcl/tk
Dear all; I would like to draw a graph with vertices and edges, and I guess tcl/tk would be appropriate. I don't know tcl/tk but have googled for a 10-page (or so) introduction to 'getting started with tcl/tk in R' but without any luck. - Does anyone know of the existence of such a document or any other web-based material on the subject? - Does anyone have an (informative) piece of code which does something similar to what I want? Thanks in advance S??ren
Peter Dalgaard
2005-Jul-31 12:45 UTC
[R] Drawing a graph with vertices and edges using tcl/tk
S??ren H??jsgaard <Soren.Hojsgaard at agrsci.dk> writes:> Dear all; I would like to draw a graph with vertices and edges, and I guess tcl/tk would be appropriate. I don't know tcl/tk but have googled for a 10-page (or so) introduction to 'getting started with tcl/tk in R' but without any luck. > - Does anyone know of the existence of such a document or any other web-based material on the subject? > - Does anyone have an (informative) piece of code which does something similar to what I want? > Thanks in advance > S??renEr, one might have expected that you'd be aware of the fact that Jens Henrik's dynamicGraph package is implemented with tcltk. If you want something less entangled in S4 classes, here's a bit of code that goes back to the early gR days. Still seems to work: graphdiddle <- function(X,Y,Labels,from,to) { if (length(X)!=length(Y) || length(from)!=length(to)) stop("invalid data") top <- tktoplevel() tktitle(top) <- "Graph diddler" canvas <- tkcanvas(top, relief="raised", width=400, height=400) tkpack(canvas) moveNode <- function(i) { force(i) function(x,y){ x <- as.numeric(x) y <- as.numeric(y) for ( e in nodeEdges[[i]] ){ tkcoords(canvas,e$edgeItem,x,y,X[e$to],Y[e$to]) } tkmove(canvas, nodeItem[i], x-X[i],y-Y[i]) X[i] <<- x Y[i] <<- y } } nodeEdges <- vector("list",length(x)) nodeItem <- vector("character",length(x)) for ( i in seq(along=from) ) { f <- from[i] t <- to[i] # add line to canvas e <- tkcreate(canvas, "line", X[f],Y[f],X[t],Y[t], width=2) nodeEdges[[f]] <- c(nodeEdges[[f]],list(list(to=t, edgeItem=e))) nodeEdges[[t]] <- c(nodeEdges[[t]],list(list(to=f, edgeItem=e))) } for ( i in seq(along=x) ) { # add the nodes p <- tkcreate(canvas,"oval",X[i]-6,Y[i]-6,X[i]+6,Y[i]+6, fill="red") l <- tkcreate(canvas,"text", X[i]+6, Y[i], text=Labels[i], anchor="nw", font="10x20") tag <- paste("node",i,sep="") tkaddtag(canvas, tag, "withtag", p) tkaddtag(canvas, tag, "withtag", l) nodeItem[i] <- tag # animate them tkitembind(canvas, p, "<B1-Motion>", moveNode(i)) } } # test code library(tcltk) x <- c(100,200,300,200) y <- c(100,200,300,300) lbl <- c("sex", "drug", "wok", "wool") from <- c(1,2,3) to <- c(2,3,4) graphdiddle(x,y,lbl,from,to) -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Gabor Grothendieck
2005-Jul-31 13:49 UTC
[R] Drawing a graph with vertices and edges using tcl/tk
Another approach is the Bell Labs graphviz system. See http://www.graphviz.com For an example of using it from R, see the dot.proto function in the 'proto' package. dot.proto generates dot language output to draw a graphviz graph of the objects in any R program written using 'proto'. library(proto) example(dot.proto) # see ?dot.proto for instructions on displaying graph The above approach should work on any OS supported by R (since graphviz works on all of them); however, on UNIX one could also use the Rgraphviz package that specifically interfaces R with graphviz: http://www.bioconductor.org/repository/release1.4/package/html/Rgraphviz.html On 7/31/05, S??ren H??jsgaard <Soren.Hojsgaard at agrsci.dk> wrote:> Dear all; I would like to draw a graph with vertices and edges, and I guess tcl/tk would be appropriate. I don't know tcl/tk but have googled for a 10-page (or so) introduction to 'getting started with tcl/tk in R' but without any luck. > - Does anyone know of the existence of such a document or any other web-based material on the subject? > - Does anyone have an (informative) piece of code which does something similar to what I want? > Thanks in advance > S??ren > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Dirk Koschuetzki
2005-Jul-31 15:51 UTC
[R] Drawing a graph with vertices and edges using tcl/tk
Hello, On Sun, 31 Jul 2005 13:46:31 +0200, S??ren H??jsgaard <Soren.Hojsgaard at agrsci.dk> wrote:> Dear all; I would like to draw a graph with vertices and edgesAnother option besides graphviz and Rgraphviz is the sna package from cran. Look into the gplot function, perhaps it is what you need. Cheers, Dirk