Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line width #3

Open
kasperdanielhansen opened this issue Mar 27, 2015 · 0 comments
Open

line width #3

kasperdanielhansen opened this issue Mar 27, 2015 · 0 comments

Comments

@kasperdanielhansen
Copy link
Owner

I am struggling with Rgraphviz to make it draw edges of different widths. You will find a more detailed description of the problem (along with example code) in this question:
http://stackoverflow.com/questions/29282522/setting-edge-width-in-rgraphviz

Apparently, there is a slot in edge attributes called "lwd" which controls the line width. It is documented on the AgEdge man page:

‘lwd’: Object of class ‘"numeric"’ The edge line width.

However, it is not possible to set it using edgeAttrs option to agopen() or plot():

  • simply specifying it in edgeAttrs (like you can specify the colors of the edges) throws an error, because getDefaultAttrs() doesn't return a default setting for lwd.

  • it is possible to provide a default value using the following syntax:

    eAttrs <- list( lwd=list( "AC"=5, "AB"=1 ) )
    rag <- agopen( graph, "", edgeAttrs=eAttrs, attrs= list(edge=list(lwd=2)) )

However, this one produces an incorrect graph. agopen() as a chunk of code at the end which ignores the names of the edges and simply copies the lwd's from first to last, resulting in an effectively random assignment to the edges.

Unfortunately, I cannot provide any patches. However, here is a little function I wrote that can set any attribute on the graph. Dangerous -- it does not check for correctness of the arguments, ignores directions of nodes (my graphs are all directionless), so it is not of general interest.

setEdgeAttr <- function( graph, attribute, value, ID1, ID2 ) {
  idfunc <- function(x) paste0( sort(x), collapse="~" )
  all.ids <- sapply( AgEdge(graph),
        function(e) idfunc( c( attr(e, "head"), attr( e, "tail" ))))

  sel.ids <- apply(cbind( ID1, ID2 ), 1, idfunc )

  if(!all(sel.ids %in% all.ids)) stop( "only existing edges, please" )
  sel <- match( sel.ids, all.ids )

  for(i in 1:length(sel)) {
    attr( attr( graph, "AgEdge" )[[ sel[i] ]], attribute ) <- value[i]
   }

   return(graph)
 }

Example usage:

nodes <- c( LETTERS[1:3] )
edgesL <- list( A=c("B", "C"), B=c("A", "C"), C=c("B", "A" ) )
graph <- new( "graphNEL", nodes= nodes, edgemode="undirected", edgeL=edgesL )
rag <- agopen( graph, "" )
rag <- setEdgeAttr( rag, "lwd", c(5, 20), c("B", "B"), c( "A", "C" ) )

plot(rag)

Cheers,

j.

(submitted by January Weiner [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant