Last active
August 29, 2015 14:13
-
-
Save timelyportfolio/d6390e3c83020c8243f1 to your computer and use it in GitHub Desktop.
example of how to define graph in DiagrammeR for graphviz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 @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
Awesome - thanks muchly, this is terrific!