Rgraphviz: interactive graphs, how to map coordinates to plotted graph?
1
0
Entering edit mode
@rainer-machne-3648
Last seen 10.3 years ago
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
graph Rgraphviz graph Rgraphviz • 1.3k views
ADD COMMENT
0
Entering edit mode
@wolfgang-huber-3550
Last seen 3 months ago
EMBL European Molecular Biology Laborat…
Dear Rainer Have a look at the 'renderGraph' function: showMethods("renderGraph", includeDefs=TRUE) and at r1 <- renderGraph(g1) graphRenderInfo(r1) Florian might be able to tell you more on how to map the output of that to the user coordinates of the R graphics device. Best wishes Wolfgang Btw, a "poor man's" interactive plot (in static html) can be made like this: http://www.ebi.ac.uk/~huber/tooltips/IMCA/imca.R resulting in http://www.ebi.ac.uk/~huber/tooltips/IMCA/imca.html ------------------------------------------------------- Wolfgang Huber EMBL http://www.embl.de/research/units/genome_biology/huber ------------------------------------------------------- Machne ha scritto: > 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 > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor --
ADD COMMENT

Login before adding your answer.

Traffic: 486 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6