[R] Drawing a graph with vertices and edges using tcl/tk

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Jul 31 14:45:46 CEST 2005


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øren

Er, 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




More information about the R-help mailing list