rViewGraph  R Documentation 
Creates and starts an animated graphical user interface (GUI) for positioning the vertices of a graph in 2 dimensions.
rViewGraph(object, names, cols, shapes, layout, directed, running, ...)
## Default S3 method:
rViewGraph(
object,
names = NULL,
cols = "yellow",
shapes = 0,
layout = NULL,
directed = FALSE,
running = TRUE,
...
)
## S3 method for class 'igraph'
rViewGraph(
object,
names = igraph::V(object)$name,
cols = "yellow",
shapes = 0,
layout = igraph::layout.random(object),
directed = igraph::is.directed(object),
running = TRUE,
...
)
object 
the object specifying the graph. This can be specified in various ways:

names 
the names of the vertices. This is an object that can be interpreted
as a vector of
strings that will be used to label the vertices. If the length is less than the number of
vertices, the names will be cycled. The default is 
cols 
the colours of the vertices. This is on object that can be
interpreted as a vector of colours specified in the usual 
shapes 
the shapes of the vertices. This is a vector of integers specifying the shapes of the vertices. The available shapes are:
The default is 
layout 
the starting positions of the vertices. This is an 
directed 
indicates whether or not the graph is directed. 
running 
indicates whether or not to start with the animation running. 
... 
passed along extra arguments. 
Creates and starts a 'Java' GUI showing a real time animation of a NewtonRaphson optimization of a force function specified between the vertices of an arbitrary graph. There are attractive forces between adjacent vertices and repulsive forces between all vertices. The repulsions go smoothly to zero in a finite distance between vertices so that, unlike some other methods, different components don't send each other off to infinity.
The program is controlled by a slide bar, some buttons, the arrow, home and shift keys, but mostly by mouse operations. All three mouse buttons are used. The interactive mouse, key and slide bar operations are described below.
rViewGraph
is intended only for interactive use. When used in a noninteractive environment
it immediately exits returning the value NULL
.
Otherwise, all versions of rViewGraph
return a list of functions that control the actions of
the interactive viewer.
run() 
Starts the GUI running if it's not already doing so. 
stop() 
Stops the GUI running if it's running. 
hide() 
Stops the GUI and hides it. 
show() 
Shows the GUI. If it was running when 
getLayout() 
Returns the coordinates of the vertices as currently shown in the GUI.
These are given as an 
setLayout(layout = NULL) 
Sets the coordinates of the vertices to the given values. 
hidePaper() 
By default the GUI indicates, with a different colour, the portion of the plane that corresponds to the current choice of paper for printing. This function removes that area. 
showPaper(size = "letter", landscape = TRUE) 
Indicates, with a different colour, the portion of the
plane corresponding to a choice of paper for printing. 
hideAxes() 
By default, axes are shown to indicate the origin. This function hides them. 
showAxes() 
Shows the axes if they are hidden. 
writePostScript() 
This starts a 
ps() 
Alias for 
Slide bars at the bottom of the GUI control the repulsive force in the energy equation used to set the coordinates. If the graph is undirected, there is a single 'Repulsion' parameter, if directed, there are 'XRepulsion' and 'YRepulsion' parameters, and a 'Gravity' parameter that influences how these are combined.
Mouse operations without shift key and without control key pressed.
Left mouse: Drags a vertex. Vertex is free on release.
Middle mouse: Drags a vertex. Vertex is fixed at release position.
Right mouse: Translates the view by the amount dragged. A bit like putting your finger on a piece of paper and moving it.
Double click with any mouse button in the background: Resets the vertices to new random positions.
Mouse operations with shift key but without control key pressed.
Left mouse: Drags a vertex and the component it is in. Vertex and component free on release.
Middle mouse: Drags a vertex and the component it is in. Vertex and component are fixed at release positions.
Right mouse: Translates the positions of the vertices relative to the position of the canvas by the amount dragged. This is useful to center the picture on the canvas ready for outputting.
Mouse operations without shift key but with control key pressed.
Left mouse: Click on a vertex to unhide any hidden neighbours.
Middle mouse: Click on a vertex to hide it.
Double click left mouse: Unhides all hidden vertices.
Double click middle mouse: Hides all vertices.
Mouse operations with shift key and with control key pressed.
Left mouse: Click on a vertex to unhide all vertices in the same component.
Middle mouse: Click on a vertex to hide it and the component it is in.
Key functions without shift key pressed. Mouse has to be in the picture canvas.
Up arrow: Increases the scale of viewing by 10%.
Down arrow: Decreases the scale of viewing by 10%.
Left arrow: Rotates the view by 15 degrees clockwise.
Right arrow: Rotates the view by 15 degrees anticlockwise.
Home key: Undoes all scalings and rotations and places the origin at the top left corner of the canvas.
Key functions with shift key pressed. Mouse has to be in the picture canvas.
Up arrow: Increases the vertex positions by 10% relative to the scale of the canvas.
Down arrow: Decreases the vertex positions by 10% relative to the scale of the canvas.
Left arrow: Rotates the vertex positions by 15 degrees clockwise relative to the canvas orientation.
Right arrow: Rotates the vertex positions by 15 degrees anticlockwise relative to the canvas orientation.
Alun Thomas
A full description of the force function and algorithm used is given by C Cannings and A Thomas, Inference, simulation and enumeration of genealogies. In D J Balding, M Bishop, and C Cannings, editors, The Handbook of Statistical Genetics. Third Edition, pages 781805. John Wiley & Sons, Ltd, 2007.
require(rviewgraph)
# First generate the random edges of an Erdos Renyi random graph.
f = sample(100,size=200,replace=TRUE)
t = sample(100,size=200,replace=TRUE)
# The following should all show the same graph:
# ... specified as a two column matrix.
v1 = rViewGraph(cbind(f,t))
# ... in 'igraph' preferred format.
v2 = rViewGraph(c(f,t))
# ... as an adjacency matrix.
x = matrix(0,ncol=max(f,t),nrow=max(f,t))
for (i in 1:length(f)) x[f[i],t[i]] = 1
v3 = rViewGraph(x)
# Specifying names, colours and shapes.
# Use unlabeled vertices, as red, green and blue diamonds.
v4 = rViewGraph(cbind(f,t), names = " ", cols = c(2,3,4), shapes=2)
# Use yellow vertices with random shapes, labeled with capital letters.
y = matrix(sample(1:26,100,TRUE),ncol=2)
v5 = rViewGraph(y,names=LETTERS,cols="cyan",shapes=sample(0:2,26,TRUE))
# Controlling a currently active GUI.
if (!is.null(v5))
{
# Shift the coordinates, although this is more
# easily done with mouse controls.
v5$setLayout(100 + v5$getLayout())
# Reset the coordinates to random values.
v5$setLayout()
# Pepare a plot for printing, fix it, and start a PostScript print job.
v5$hideAxes()
v5$showPaper("A3",F)
v5$stop()
v5$writePostScript()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.