This is a wrapper to layout graph objects using arbitrary layout
engines. The default engine (and so far the only implemented engine)
is ATT's Graphviz.
Usage
layoutGraph(x, layoutFun = layoutGraphviz, ...)
Arguments
x
A graph object
layoutFun
A function that performs the graph layout and
returns a graph object with all necessary rendering information
...
Further arguments that are passed to layoutFun
Details
Layout of a graph and its rendering are two separate
processes. layoutGraph provides an API to use an arbitrary
algorithm for the layout. This is archived by abstraction of the
layout process into a separate function (layoutFun) with
well-defined inputs and outputs. The only requirements on the
layoutFun are to accept a graph object as input and to return a
valid graph object with all the necessary rendering information stored
in its renderInfo slot. This information comprises
for nodes:
nodeX, nodeY
the locations of the nodes, in the coordinate
system defined by bbox (see below).
lWidth, rWidth
the width components of the nodes,
lWidth+rWidth=total width.
height
the heights of the nodes.
labelX, labelY
node label locations.
labelJust
the justification of the node labels.
label
node label text.
shape
the node shape. Valid values are box,
rectangle, ellipse, plaintext, circle
and triangle.
for edges:
splines
representation of the edge splines as a list of
BezierCurve objects.
labelX, labelY
edge label locations.
label
edge label text.
arrowhead, arrowtail
some of Graphviz's arrow shapes are
supported. Currently they are: open, normal,
dot, odot, box, obox, tee,
diamond, odiamond and none. In addition, a
user-defined function can be passed which needs to be able to deal
with 4 arguments: A list of xy coordinates for the center of the
arrowhead, and the graphical parameters col, lwd and
lty.
direction
The edge direction. The special value both is
used when reciprocrated edges are to be collapsed.
To indicate that this information has been added to the graph, the
graph plotting function should also set the laidout flag in the
graphData slot to TRUE and add the bounding box
information (i.e., the coordinate system in which the graph is laid
out) in the format of a two-by-two matrix as item bbox in the
graphData slot.
AT&T's Graphviz is the default layout algorithm to use when
layoutGraph is called without a specific layoutFun
function. See agopen for details about how to tweak
Graphviz and the valid arguments that can be passed on through
.... The most common ones to set in this context might be
layoutType, which controls the type of layout to compute and
the nodeAttrs and edgeAttrs arguments, which control the
fine-tuning of nodes and edges.
Value
An object inheriting from class graph
Note
Please note that the layout needs to be recomputed whenever attributes
are changed which are bound to affect the position of nodes or
edges. This is for instance the case for the arrowhead and
arrowtail parameters.
set.seed(123)
V <- letters[1:5]
M <- 1:2
g1 <- randomGraph(V, M, 0.5)
edgemode(g1) <- "directed"
x <- layoutGraph(g1)
renderGraph(x)
## one of Graphviz's additional layout algorithms
x <- layoutGraph(g1, layoutType="neato")
renderGraph(x)
## some tweaks to Graphviz's node and edge attributes,
## including a user-defined arrowhead and node shape functions.
myArrows <- function(x, ...)
{
for(i in 1:4)
points(x,cex=i)
}
myNode <- function(x, col, fill, ...)
symbols(x=mean(x[,1]), y=mean(x[,2]), thermometers=cbind(.5, 1,
runif(1)), inches=0.5,
fg=col, bg=fill, add=TRUE)
eAtt <- list(arrowhead=c("a~b"=myArrows, "b~d"="odiamond", "d~e"="tee"))
nAtt <- list(shape=c(d="box", c="ellipse", a=myNode))
edgemode(g1) <- "directed"
x <- layoutGraph(g1, edgeAttrs=eAtt, nodeAttrs=nAtt, layoutType="neato")
renderGraph(x)
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Rgraphviz)
Loading required package: graph
Loading required package: BiocGenerics
Loading required package: parallel
Attaching package: 'BiocGenerics'
The following objects are masked from 'package:parallel':
clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
clusterExport, clusterMap, parApply, parCapply, parLapply,
parLapplyLB, parRapply, parSapply, parSapplyLB
The following objects are masked from 'package:stats':
IQR, mad, xtabs
The following objects are masked from 'package:base':
Filter, Find, Map, Position, Reduce, anyDuplicated, append,
as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
rbind, rownames, sapply, setdiff, sort, table, tapply, union,
unique, unsplit
Loading required package: grid
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/Rgraphviz/layoutGraph.Rd_%03d_medium.png", width=480, height=480)
> ### Name: layoutGraph
> ### Title: A function to compute layouts of graph objects
> ### Aliases: layoutGraph
> ### Keywords: graphs
>
> ### ** Examples
>
> set.seed(123)
> V <- letters[1:5]
> M <- 1:2
> g1 <- randomGraph(V, M, 0.5)
> edgemode(g1) <- "directed"
> x <- layoutGraph(g1)
> renderGraph(x)
>
> ## one of Graphviz's additional layout algorithms
> x <- layoutGraph(g1, layoutType="neato")
> renderGraph(x)
>
> ## some tweaks to Graphviz's node and edge attributes,
> ## including a user-defined arrowhead and node shape functions.
> myArrows <- function(x, ...)
+ {
+ for(i in 1:4)
+ points(x,cex=i)
+ }
>
> myNode <- function(x, col, fill, ...)
+ symbols(x=mean(x[,1]), y=mean(x[,2]), thermometers=cbind(.5, 1,
+ runif(1)), inches=0.5,
+ fg=col, bg=fill, add=TRUE)
>
> eAtt <- list(arrowhead=c("a~b"=myArrows, "b~d"="odiamond", "d~e"="tee"))
> nAtt <- list(shape=c(d="box", c="ellipse", a=myNode))
> edgemode(g1) <- "directed"
> x <- layoutGraph(g1, edgeAttrs=eAtt, nodeAttrs=nAtt, layoutType="neato")
> renderGraph(x)
>
>
>
>
>
>
> dev.off()
null device
1
>