1
votes

I would like to create this simple igraph plot:

library(igraph)
mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"))
mygraph <- graph_from_data_frame(d=mydata, directed=T)
plot(mygraph, vertex.label.dist=2)

enter image description here

With diagrammeR

library(DiagrammeR)
mygraph2 <- from_igraph(mygraph)
grViz(mygraph2)

Produces this error

Error in file.exists(diagram) : invalid 'file' argument

I've also tried with

grViz(readLines(mygraph2)) 

and other combinations or the command plot() but I can't find the proper way.

How can I do it?

I've openen a new question to get the same result directly with DiagrammeR, without igraph:

How to create a network graph with DiagrammeR?

2
Do you want render_graph ? - user20650

2 Answers

3
votes

There seems to be a couple of things going on.

library(igraph)
library(DiagrammeR)

mydata <- data.table(from=c("John", "John", "Jim"),to=c("John", "Jim", "Jack"))
mygraph <- graph_from_data_frame(d=mydata, directed=TRUE)

The following code throws an warning

mygraph2 <- from_igraph(mygraph)

Warning messages: 1: In data.frame(from = as.integer(igraph::ends(igraph, igraph::E(igraph))[, : NAs introduced by coercion

And if you look at mygraph2 there is no node or edge info, and it doesnt render : render_graph(mygraph2). But the warning is informative as it points to the lines of code ( as.integer(ends(mygraph, E(mygraph), names=TRUE)) : maybe we want names=FALSE) , so try removing names , but set labels

V(mygraph)$label = V(mygraph)$name
V(mygraph)$name = factor(V(mygraph)$name, levels=as.character(V(mygraph)$name))

No warning and renders

mygraph2 <- from_igraph(mygraph)
render_graph(mygraph2)

If you want to see the dot code you can use generate_dot , and then pass this to grViz, however, this is what render_graph is doing.

grViz(generate_dot(mygraph2))
1
votes

There are two problems in your process.

The first one is in the command from_igraph. I'm not sure, maybe it's a bug in the package, maybe it's a problem with my setting, but I couldn't use it to get the desired result. The following works on my machine.

mygraph3 <- from_adj_matrix(as.matrix(get.adjacency(mygraph)), mode = "directed")

And then you need render_graph or something like that to get your graph, grViz is for string representation of graph, not for graph itself.

render_graph(mygraph3)