tographviz: Convert a DAG into graphviz format

tographvizR Documentation

Convert a DAG into graphviz format


Given a matrix defining a DAG create a text file suitable for plotting with graphviz


toGraphviz(dag, data.df=NULL, data.dists=NULL, group.var=NULL, outfile, directed=TRUE)



a matrix defining a DAG.


a data frame containing the data used for learning the network.


a list with named arguments matching the names of the data frame which gives the distribution family for each variable. See fitAbn for details.


only applicable for mixed models and gives the column name in data.df of the grouping variable (which must be a factor denoting group membership). See fitAbn for details.


a character string giving the filename which will contain the graphviz graph.


logical; if TRUE, a directed acyclic graph is produced, otherwise an undirected graph.


Graphviz ( is visualisation software developed by AT&T and freely available. This function creates a text representation of the DAG, or the undirected graph, so this can be plotted using graphviz. The R package, Rgraphviz (available through the Bioconductor project interfaces R and the working installation of graphviz.
Binary nodes will appear as squares, Gaussian as ovals and Poisson nodes as diamonds in the resulting graphviz network diagram. There are many other shapes possible for nodes and numerous other visual enhancements - see online graphviz documentation. Bespoke refinements can be added by editing the raw outfile produced. For full manual editing, particularly of the layout, or adding annotations, one easy solution is to convert a postscript format graph (produced in graphviz using the -Tps switch) into a vector format using a tool such as pstoedit (, and then edit using a vector drawing tool like xfig. This can then be resaved as postscript or pdf thus retaining full vector quality.


Nothing is returned, but a file outfile written.


Fraser Iain Lewis


Further information about abn can be found at:


## On a typical linux system the following code constructs a nice
## looking pdf file 'graph.pdf'.
## Not run: 
## Subset of a build-in dataset
mydat <-[,c("b1","b2","b3","g1","b4","p2","p4")]

## setup distribution list for each node
mydists <- list(b1="binomial", b2="binomial", b3="binomial",
                g1="gaussian", b4="binomial", p2="poisson",
## specify DAG model
mydag <- matrix(c(   0,1,0,0,1,0,0, #
                     0,0,0,0,0,0,0, #
                     0,1,0,0,1,0,0, #
                     1,0,0,0,0,0,1, #
                     0,0,0,0,0,0,0, #
                     0,0,0,1,0,0,0, #
                     0,0,0,0,1,0,0  #
                     ), byrow=TRUE, ncol=7)

colnames(mydag) <- rownames(mydag) <- names(mydat)

## create file for processing with graphviz
outfile <- paste(tempdir(), "", sep="/")
toGraphviz(dag=mydag, data.df=mydat, data.dists=mydists, outfile=outfile)
## and then process using graphviz tools e.g. on linux
# system(paste( "dot -Tpdf -o graph.pdf", outfile))
# system("evince graph.pdf")

## Example using data with a group variable  where b1<-b2
mydag <- matrix(c(0,1, 0,0), byrow=TRUE, ncol=2)

colnames(mydag) <- rownames(mydag) <- names([,c(1,2)])
## specific distributions
mydists <- list(b1="binomial", b2="binomial")

## create file for processing with graphviz
outfile <- paste0(tempdir(), "/")
toGraphviz(dag=mydag,[,c(1,2,14)], data.dists=mydists,
           outfile=outfile, directed=FALSE)
## and then process using graphviz tools e.g. on linux:
# pdffile <- paste0(tempdir(), "/graph.pdf")"
# system(paste("dot -Tpdf -o ", pdffile, outfile))
# system(paste("evince ", pdffile, " &")   ## or some other viewer

## End(Not run)

abn documentation built on April 25, 2022, 9:06 a.m.