1################################################################################ 2# Demo of gexf function 3# Author: Jorge Fabrega 4################################################################################ 5pause <- function() { 6 invisible(readline("\nPress <return> to continue: ")) 7} 8pause <- function() NULL 9pause() 10 11# Defining a matrix of nodes 12pause() 13people <- data.frame(id=1:4, label=c("juan", "pedro", "matthew", "carlos")) 14people 15 16# Defining a matrix of edges 17pause() 18 19relations <- data.frame(source=c(1,1,1,2,3,4,2,4,4), 20 target=c(4,2,3,3,4,2,4,1,1)) 21relations 22 23# Defining a matrix of dynamics (start, end) for nodes and edges 24pause() 25 26time.nodes <- data.frame(matrix(c(10.0,13.0,2.0,2.0,12.0,rep(NA,3)), 27 nrow=4, ncol=2)) 28time.nodes 29 30time.edges<- data.frame(matrix(c(10.0,13.0,2.0,2.0,12.0,1,5,rep(NA,5), 31 rep(c(0,1),3)), ncol=2)) 32time.edges 33 34# Defining a data frame of attributes for nodes and edges 35pause() 36 37node.att <- data.frame(letrafavorita=c(letters[1:3],"hola"), numbers=1:4) 38node.att 39 40edge.att <- data.frame(letrafavorita=letters[1:9], numbers=1:9) 41edge.att 42 43################################################################################ 44# First example: a simple net 45pause() 46gexf(nodes=people, edges=relations) 47 48################################################################################ 49# Second example: a simple net with nodes attributes 50pause() 51gexf(nodes=people, edges=relations, nodesAtt=node.att) 52 53################################################################################ 54# Third example: a simple net with dynamic nodes 55pause() 56gexf(nodes=people, edges=relations, nodeDynamic=time.nodes) 57 58################################################################################ 59# Fourth example: a simple net with dynamic nodes with attributes 60pause() 61gexf(nodes=people, edges=relations, nodeDynamic=time.nodes, nodesAtt=node.att) 62 63################################################################################ 64# Fifth example: a simple net with dynamic edges with attributes 65pause() 66gexf(nodes=people, edges=relations, edgeDynamic=time.edges, edgesAtt=edge.att) 67 68################################################################################ 69# Sixth example: a simple net with dynamic edges and nodes with attributes 70pause() 71gexf(nodes=people, edges=relations, edgeDynamic=time.edges, edgesAtt=edge.att, 72 nodeDynamic=time.nodes, nodesAtt=node.att) 73 74################################################################################ 75# Seventh example: a simple net with dynamic edges and nodes with attributes 76pause() 77imagee <- data.frame(image=rbind( 78 "Yellow_solid_sphere.png", 79 "Yellow_solid_sphere.png", 80 "Yellow_solid_sphere.png", 81 "Yellow_solid_sphere.png"), stringsAsFactors=F) 82 83# Colors 84nodecolors <- cbind(t(col2rgb(topo.colors(nrow(people)))),alpha=1) 85colnames(nodecolors) <- c("r", "b", "g", "a") 86 87edgecolors <- cbind(t(col2rgb(cm.colors(nrow(relations)))),alpha=1) 88colnames(edgecolors) <- c("r", "b", "g", "a") 89 90# TRUE/FALSE attributes 91nodetruefalse <- data.frame(nodetrue=rnorm(NROW(people)) > 0) 92edgetruefalse <- data.frame(edgetrue=rnorm(NROW(relations)) > 0) 93 94# We now create the graph object 95pause() 96grafo <- gexf(nodes=people, edges=relations, 97 nodesAtt=cbind(imagee,nodetruefalse), 98 nodesVizAtt=list( 99 shape=c("rectangle", "square", "triangle", "diamond"), 100 position=matrix(1:12,nrow=4), 101 image=imagee, 102 color=nodecolors, 103 size=c(1,4,10,30) 104 ), 105 edgesVizAtt=list( 106 size=1:9, 107 color=edgecolors 108 ), 109 edgesAtt=edgetruefalse) 110 111# And print it 112grafo 113