[BioC] Rgraphviz: interactive graphs, how to map coordinates to plotted graph?

Rainer Machne raim at tbi.univie.ac.at
Mon Aug 31 08:40:48 CEST 2009


Hi,

I want to use interactive features of R ("identify()") on Rgraphviz
plots, and also plot other things over the plotted graph (using node
coordinates) after it has been rendered. However, the plotted
coordinates are shifted with respect to the coordinates returned by
nodeRenderInfo(graph)$nodeX/nodeY.

Does anyone know an easy way to calculate this shift, avoid the shift,
or reset the plot area to coordinates used for rendering the graph? 
Or: are there already packages providing interactivity for
Rgraphviz-rendered graphs?

Below you find a small example script, including a hack of the function
identifyPch() provided by the help page "?identify" to work on
renderGraph(graph) plots, here for a graph object from the ?randomGraph
help page example.
This should illustrate well what i want to do and what the problem is.

The green "x" should be on the nodes but are shifted. The
identifyNode(graph) function would allow to click on nodes (here the
"x"), plot something over them, and e.g. return a list of selected
nodes. I think this would open up the graph and Rgraphviz packages for a
lot of nice applications. 

Rainer


# graphCoordinates.R

library('Rgraphviz')

## TODO : find out how we can set coordinates to allow
## usage of identify() function in Rgraphviz graphs
identifyNode <- function(graph, pch=19, showNodeXY=TRUE, ...)
  {

    xy <- cbind(nodeRenderInfo(graph)$nodeX,nodeRenderInfo(graph)$nodeY)

    # show where x/y coordinates would put nodes
    if (showNodeXY)
      points(xy, pch=4, col=3)
 
    n <- nrow(xy)
    
    x <- xy[,1]
    y <- xy[,2]
    
    sel <- rep(FALSE, length(x)); res <- integer(0)
    while(sum(sel) < n) {
      ans <- identify(x[!sel], y[!sel], n=1, plot=FALSE, ...)
      if(!length(ans)) break
      ans <- which(!sel)[ans]
      points(x[ans], y[ans], pch = pch, col=2)

      cat(paste("SELECTED NODE", rownames(xy)[ans], "\n"))
      
      sel[ans] <- TRUE
      res <- c(res, ans)
    }
    res
  }

## 1) generate random graph, from ?randomGraph help page:
set.seed(123)
V <- letters[1:10]
M <- 1:4
g1 <- randomGraph(V, M, 0.2)
numEdges(g1) # 16, in this case
edgeNames(g1)# "<from> ~ <to>"  since undirected

## 2) layout and plot the graph
g1 <- layoutGraph(g1)
renderGraph(g1)


## 3) Identify nodes:
## After calling identifyNode(g1) please click on the graph 
## to identify nodes.
## The green "x" are plotted at x/y coordinates in nodeRenderInfo
## Note that they are shifted wrt to the rendered nodes.

identifyNode(g1) 

# End of file



More information about the Bioconductor mailing list