Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(Rgraphviz)
- # Assumes that a graph is a data frame with columns
- # subject, object, [label, width, hue, saturation, brightness]
- namedvector <- function(names, values) {names(values) = names; values}
- render <- function(graph) {
- nodes = union(graph$subject, graph$object)
- edges = tapply(graph$object, graph$subject, function(x) list(edges=as.character(x)), simplify=F)
- # add edges for object-only nodes (leafs)
- for (leaf in setdiff(graph$object, graph$subject)) edges[[leaf]] = list(edges=NULL)
- # create graph
- g <- new("graphNEL", nodes = nodes, edgeL = edges, edgemode="directed")
- # add labels, width, color if given
- edgeids = paste(graph$subject, graph$object, sep="~")
- edgeAttrs = if (is.null(graph$label)) list() else
- list(label=namedvector(edgeids, as.character(graph$label)) )
- if (!is.null(graph$col))
- edgeRenderInfo(g) <- list(col=namedvector(edgeids, as.character(graph$col)))
- if (!is.null(graph$width))
- edgeRenderInfo(g) <- list(lwd=namedvector(edgeids, graph$width))
- if (!is.null(graph$label))
- edgeRenderInfo(g) <- list(label=namedvector(edgeids, as.character(graph$label)))
- # layour and render
- g = layoutGraph(g, recipEdges="distinct", edgeAttrs=edgeAttrs)
- renderGraph(g)
- }
- # test code: create normal and reciprocal graph and some attributes
- graph = data.frame(subject=c("a", "c"), object=c("a", "a"))
- rgraph = data.frame(subject=c("a", "b"), object=c("b", "a"))
- label=c("0.7","-1.0")
- col=c("blue","red")
- width=c(2,4)
- # plot graphs in rows with increasing #features complexity in columns
- layout(matrix(1:8, nrow=2, byrow=T))
- for (g in list(graph, rgraph)) {
- render(g)
- render(cbind(g, label=label))
- render(cbind(g, label=label, width=width))
- render(cbind(g, label=label, width=width, col=col))
- }
- # result at http://imageshack.us/photo/my-images/405/recip.png/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement