Created
May 19, 2015 16:11
-
-
Save timelyportfolio/7f3309b8f216fd7301b3 to your computer and use it in GitHub Desktop.
playing with R igraph, DiagrammeR, and rgexf
This file contains 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) | |
library(rgexf) | |
library(igraph) | |
library(pipeR) | |
###### DiagrammeR example from http://rich-iannone.github.io/DiagrammeR/graphs.html #### | |
### | |
# Create a graph | |
### | |
nodes <- | |
create_nodes(nodes = LETTERS, | |
type = "letter", | |
shape = sample(c("circle", "rectangle"), | |
length(LETTERS), | |
replace = TRUE), | |
fillcolor = sample(c("aqua", "gray80", | |
"pink", "lightgreen", | |
"azure", "yellow"), | |
length(LETTERS), | |
replace = TRUE)) | |
edges <- | |
create_edges(edge_from = sample(LETTERS, replace = TRUE), | |
edge_to = sample(LETTERS, replace = TRUE), | |
relationship = "letter_to_letter") | |
graph <- | |
create_graph(nodes_df = nodes, | |
edges_df = edges, | |
graph_attrs = "layout = neato", | |
node_attrs = c("fontname = Helvetica", | |
"style = filled"), | |
edge_attrs = c("color = gray20", | |
"arrowsize = 0.5")) | |
# View the graph in the RStudio Viewer | |
render_graph(graph) | |
#### now try to plot with rgexf ##### | |
write.gexf( | |
nodes = graph$nodes_df[,c(1,1)] | |
,edges = graph$edges_df[,1:2] | |
,nodesVizAtt = data.frame(shape=rep("diamond",nrow(graph$nodes_df))) | |
) %>>% plot | |
#### play with igraph example and rgexf #### | |
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David", | |
"Esmeralda"), | |
age=c(48,33,45,34,21), | |
gender=c("F","M","F","M","F")) | |
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David", | |
"David", "Esmeralda"), | |
to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"), | |
same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE), | |
friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3)) | |
g <- graph.data.frame(relations, directed=TRUE, vertices=actors) | |
print(g, e=TRUE, v=TRUE) | |
g_layout <- layout.grid(g) | |
plot(igraph.to.gexf(g,g_layout %>>% (data.frame(x=.[,1],y=-.[,2],z=0)))) | |
#### play with igraph and DiagrammeR #### | |
# use g defined in above example | |
# no layout | |
g %>>% | |
get.data.frame( what = "both" ) %>>% | |
( | |
create_graph( | |
data.frame( | |
nodes = rownames(.$vertices) | |
,.$vertices | |
) | |
,.$edges | |
) | |
) %>>% | |
render_graph( ) | |
# with layout from above | |
g %>>% | |
get.data.frame( what = "both" ) %>>% | |
( | |
create_graph( | |
data.frame( | |
nodes = rownames(.$vertices) | |
,.$vertices | |
,x = g_layout[,1] | |
,y = g_layout[,2] | |
) | |
,.$edges | |
,graph_attrs = 'splines="true" overlap = "scale"' | |
) | |
) %>>% | |
( | |
grViz( .$dot_code, engine="neato", options = list("-n") ) # don't think -n necessary | |
) | |
# with layout.circle | |
g %>>% | |
( | |
list( | |
df = get.data.frame( ., what = "both" ) | |
,layout = layout.circle(.) | |
) | |
) %>>% | |
( | |
create_graph( | |
data.frame( | |
nodes = rownames(.[[1]]$vertices) | |
,.[[1]]$vertices | |
,x = .[[2]][,1] | |
,y = .[[2]][,2] | |
) | |
,.[[1]]$edges | |
,graph_attrs = 'splines="true" overlap = "scale"' | |
) | |
) %>>% | |
( | |
grViz( .$dot_code, engine="neato", options = list("-n") ) # don't think -n necessary | |
) | |
# with layout.fruchterman.reingold | |
g %>>% | |
( | |
list( | |
df = get.data.frame( ., what = "both" ) | |
,layout = layout.fruchterman.reingold(.) | |
) | |
) %>>% | |
( | |
create_graph( | |
data.frame( | |
nodes = rownames(.[[1]]$vertices) | |
,.[[1]]$vertices | |
,x = .[[2]][,1] | |
,y = .[[2]][,2] | |
) | |
,.[[1]]$edges | |
,graph_attrs = 'splines="ortho"' | |
) | |
) %>>% | |
( | |
grViz( .$dot_code, engine="neato", options = list("-n") ) # don't think -n necessary | |
) | |
# with layout.drl | |
g %>>% | |
( | |
list( | |
df = get.data.frame( ., what = "both" ) | |
,layout = layout.drl(.) | |
) | |
) %>>% | |
( | |
create_graph( | |
data.frame( | |
nodes = rownames(.[[1]]$vertices) | |
,.[[1]]$vertices | |
,x = .[[2]][,1] | |
,y = .[[2]][,2] | |
) | |
,.[[1]]$edges | |
,graph_attrs = 'splines="true" overlap = "scale"' | |
) | |
) %>>% | |
( | |
grViz( .$dot_code, engine="neato", options = list("-n") ) # don't think -n necessary | |
) | |
# with layout.spring | |
g %>>% | |
( | |
list( | |
df = get.data.frame( ., what = "both" ) | |
,layout = layout.spring(.) | |
) | |
) %>>% | |
( | |
create_graph( | |
data.frame( | |
nodes = rownames(.[[1]]$vertices) | |
,.[[1]]$vertices | |
,x = .[[2]][,1] * 2 | |
,y = .[[2]][,2] * 2 | |
) | |
,.[[1]]$edges | |
,graph_attrs = 'splines="true" overlap="scale"' | |
) | |
) %>>% | |
( | |
grViz( .$dot_code, engine="neato", options = list("-n") ) # don't think -n necessary | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment