1

I have an adjacency matrix and want to use DiagrammeR Graphviz language to create a digraph of it.

Bellow is my adjacency matrix MM

MM<-matrix(c(1,1,0,0,0,1,1,1,1,0,0,1,1,1,0,0,1,0,1,1,0,0,0,1,1), 5, byrow=T)
colnames(MM)<-c("A", "B", "C", "D", "E")
row.names(MM)=colnames(MM)
MM
  A B C D E
A 1 1 0 0 0
B 1 1 1 1 0
C 0 1 1 1 0
D 0 1 0 1 1
E 0 0 0 1 1

Taking the information on DiagrammeR site diagrammeR Docs and I can create the digraph using the following code:

grViz("
digraph boxes_and_circles {

      # a 'graph' statement
      graph [overlap = true, fontsize = 10]

      node [shape = circle,
      fixedsize = true,
      width = 0.9] // sets as circles
        A; B; C; D; E



      # several 'edge' statements
      A->A A->B B->A B->B B->C B->D C->B C->C C->D D->B D->D D->E E->D E->E
    }")

This works fine to create the digraph from MM, but as you can see, to describe all nodes, and all nodes interactions for a bigger code will become too cumbersome, and the idea is to have it simplified.

So, my idea was to use a vector to indicate both the nodes and the nodes interaction, but this is where I fail. I tried using igraph to convert the adjacency matrix MM to graph format and from there extract the nodes interaction and try to assign that information to the previous code. Following is the code I used for this:

graphMM <- graph_from_adjacency_matrix(MM, mode = c("directed"),
        weighted = NULL, diag = TRUE, add.colnames = NULL, add.rownames = NA)
edgesMp<-as_edgelist(graphMM)
n_int<-c(0)
for(i in 1:nrow(edgesMM)){
    n_int[i]<-paste(edgesMM[i,1], edgesMM[i,2], sep = "->")}
n_int
 [1] "A->A" "A->B" "B->A" "B->B" "B->C" "B->D" "C->B" "C->C" "C->D" "D->B"
[11] "D->D" "D->E" "E->D" "E->E"

I tried to assign the n_int vector to the code of grViz as: # several 'edge' statements n_int

But this only prints the word n_int plus the indicated nodes, each inside a circle without actually using the n_int information to draw the connections between the nodes.

I am aware that on DiagrammeR site there is shown another form to create digraphs (graph_creation), which allows indicating the information using vectors, but that version does not allow for subgraph construction for node orientation (e.g. graph levels corresponding to trophic levels in ecology) which I will need.

So my question is, does anyone knows how to assign nodes names and their connections to the grViz code using a vectors?

  • how about generating the code with `Rgraphviz`? – user20650 Jul 03 '18 at 15:13
  • @user20650 the `Rgraphviz` was removed from the CRAN repository. – Daniel Pardejo Jul 03 '18 at 15:21
  • its at bioconductor : https://www.bioconductor.org/ – user20650 Jul 03 '18 at 15:22
  • I can't remember the succinct way to do this, but may give you a direction... `g <- graphAM(MM, edgemode='directed' ) ; agwrite(agopen(g, "foo", layout=FALSE) , temp<- tempfile())` . So the `dot` code is now saved in `temp`: `cat(readLines(temp), sep="\n")`. You can probably build this in to your `grViz` workflow. – user20650 Jul 03 '18 at 15:26

0 Answers0