Skip to content Skip to navigation
University of Warwick
  • Study
  • |
  • Research
  • |
  • Business
  • |
  • Alumni
  • |
  • News
  • |
  • About
  • Text only
  • |
  • Sign in
  • Search MOAC
  • Search University of Warwick
  • Search for people at Warwick
  • Search Warwick Blogs
  • Search past exam papers
  • Search video
  • More…

    Molecular Organisation and Assembly in Cells

    • About the DTC
    • Research
    • People
    • Degrees
    • Study at MOAC
    • News & Events
    • MOAC Students »
    • Peter Cock »
    • R Programming »
    • Network Graphs
    University of Warwick

    Drawing network graphs (nodes and edges) with R/BioConductor

    How do you draw network graphs in R? - the mathematical type of graph made up of nodes and edges that is.

    Several people recommended an R/BioConductor package called Rgraphviz, which is an interface to Graphviz - Graph Visualization Software. You'll need to install R, BioConductor and Graphviz first, and then install the Rgraphviz package and its dependancies from within R as follows:

    source("http://www.bioconductor.org/biocLite.R")
    biocLite("Ruuid")
    biocLite("graph")Replace broken link
    biocLite("Rgraphviz")
    

    So, documentation:

    • How To use the graph package [PDF],
    • HowTo Render A Graph Using Rgraphviz [PDF], very general.
    • HowTo layout a pathway [PDF], focuses on drawing genetic networks using a KEGG pathway as an example.

    If you are more interested in the Graphviz and its DOT file format you could try:

    • An Introduction to GraphViz and dot, includes a little bit on using Python with Graphviz.

    Here is a simple example converting an adjacency matrix into a graphAM object and plotting it. The matrix elements are taken to be the edge weights (zero being the absence of an edge), with the row and column names asssumed to be the nodes.

    > library(Rgraphviz)
    ...
    > test.matrix<-matrix(rep(c(0,1,0,0), 9), ncol=6, nrow=6)
    > rownames(test.matrix)<-c("a", "b", "c", "d", "e", "f")
    > colnames(test.matrix)<-c("a", "b", "c", "d", "e", "f")
    > test.matrix
      a b c d e f
    a 0 0 0 0 0 0
    b 1 0 1 0 1 0
    c 0 0 0 0 0 0
    d 0 1 0 1 0 1
    e 0 0 0 0 0 0
    f 1 0 1 0 1 0
    > am.graph<-new("graphAM", adjMat=test.matrix, edgemode="directed")
    > am.graph
    A graphAM graph with directed edges
    Number of Nodes = 6 
    Number of Edges = 9 
    > plot(am.graph, attrs = list(node = list(fillcolor = "lightblue"),
                                    edge = list(arrowsize=0.5)))
    [Graph]
    

    BioConductor also has a graphNEL object that uses Nodes and Edge-List internally.

    You can also use the as command to convert matrices into graphs, but using new is recommended as it gives you more control. For this simple example it makes no difference:

    > am.graph <- as(test.matrix, Class="graphAM")
    > am.graph
    A graphAM graph with directed edges
    Number of Nodes = 6 
    Number of Edges = 9
    
    > nel.graph <-as(test.matrix, Class="graphNEL")
    > nel.graph
    A graphNEL graph with directed edges
    Number of Nodes = 6 
    Number of Edges = 9 

    The as command can also convert between the NEL and AM graph objects:

    > as(am.graph, Class="graphNEL")
    A graphNEL graph with directed edges
    Number of Nodes = 6 
    Number of Edges = 9 
    
    > as(nel.graph, Class="graphAM")
    A graphAM graph with directed edges
    Number of Nodes = 6 
    Number of Edges = 9

    [R logo]
    The R Project

     

    [Graphviz logo]
    Graphviz : Graph Visualization Software

     

    [BioConductor logo]
    BioConductor
    for Rgraphviz

    MOAC DTC, Coventry House, University of Warwick, Gibbet Hill Road, Coventry, CV4 7AL
    Tel. 024 765 75808 moac2 at warwick dot ac dot uk

    How to find us

    MOAC Intranet

    EPSRC logo

    Close this email form
    Page contact: Peter Cock Last revised: Wed 6 Dec 2006
    • Sign in
    • |
    • Powered by Sitebuilder
    • |
    • © MMXII
    • |
    • Privacy
    • |
    • Accessibility