Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/d6390e3c83020c8243f1 to your computer and use it in GitHub Desktop.
Save timelyportfolio/d6390e3c83020c8243f1 to your computer and use it in GitHub Desktop.
example of how to define graph in DiagrammeR for graphviz
library(DiagrammeR)
mat <- structure(
c(0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1,
0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1,
0)
, .Dim = c(12L, 12L)
, .Dimnames = list(
c("A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "K", "L")
, c("A", "B", "C", "D",
"E", "F", "G", "H", "I", "J", "K", "L"))
)
temp <- dplyr::filter(
reshape2::melt(mat)
,value==1
)
#not the best way, but a way
# let's assume we have a source/target and ignore our prev matrix
# so we could use any edgelist
grViz(sprintf(
sociogram <- "
digraph boxes_and_circles {
# several 'node' statements
node [shape = circle, fixedsize = true, width = 0.9, color = blue] // sets as circles
%s
# several 'edge' statements
edge [color = red] // this sets all edges to be red
%s
# a 'graph' statement
graph [overlap = true, fontsize = 20]
}"
# get our nodes
,paste0(unique(as.vector(sapply(temp[,1:2],as.character))),collapse=";")
# get our edges
,paste(temp[,1],temp[,2],sep="->",collapse=";")
), engine = "circo")
# just for fun
# as a check let's use Rgraphviz
library(Rgraphviz)
plot(graphAM(mat,"directed"),"circo")
# no formatting but a way to go from Rgraphviz/graph to DiagrammeR
tf = tempfile()
toDot( graphAM(mat,"directed"), tf )
grViz( tf, engine = "circo" )
@jalapic
Copy link

jalapic commented Jan 20, 2015

As a non-expert at using sprintf, I'm just trying to work out how this works. Does the use of new lines of '%s' after the 'node' and 'edge' definitions essentially tell it to input a character string. Then the character strings are defined in order afterwards?

@timelyportfolio
Copy link
Author

sprintf is nice in that is has been defined across multiple languages. %s means a string. There are other % types also. By default, the arguments fill in the %s in order. The key change in your example was the collapse = ";" argument in paste.

@jalapic
Copy link

jalapic commented Jan 20, 2015

Awesome - thanks muchly, this is terrific!

@rich-iannone
Copy link

@jalapic @timelyportfolio to continue with your learning (and if you haven't already located it), I posted a link to the Graphviz DOT manual on the DiagrammeR README. Lots of great information there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment