Search code examples
rvisualizationgraphvizgraph-visualizationbioconductor

Rgraphviz: edge labels outside plotting region


I am trying to plot a Rgraphviz object with two edge labels. Unfortunately the labels fall outside the plot. Here is my example:

require('Rgraphviz')
set.seed(123)
g1 <- randomGraph(letters[1:10], 1:4, 0.4)
eAttrs <- list()
eAttrs$label <- c("a~g" = "I have a very long label 1", "a~i" = "and a  long label 2")
plot(g1, edgeAttrs = eAttrs)

Here is my plot:

I tried several things with no success:

1. Set a larger bounding box

z <- agopen(g1, "foo")
z@boundBox@upRight@x <- z@boundBox@upRight@x + 300
z@boundBox@upRight@y <- z@boundBox@upRight@y + 300
plot(z, edgeAttrs = eAttrs)

2. Decrease the label fontsize (not really what I want in my application, anyways)

eAttrs$labelfontsize=c("a~g"="3")
plot(g1, edgeAttrs = eAttrs)

3. Change par attributes:

par(oma=c(10,10,10,10))
plot(g1, edgeAttrs = eAttrs)

4. Change node, edge and general attributes from ?Rgraphviz::GraphvizAttributes

attrs <- list(graph=list(size=c(1, 1)))
attrs$edge$fontsize<-8 
plot(g1, edgeAttrs = eAttrs, attrs=attrs)

None of my attempts seem to work. Does anyone have an idea?


Solution

  • The problem

    Calling plot() on a graph object dispatches an S4 method (shown by getMethod("plot", "graph")), which in turn calls the function shown by typing getMethod("plot", "Ragraph"). That function contains the following rather unfortunate lines which, regardless of any related parameter settings you've made, will override them to reset the left and right margins to 0. Frustrating!

    oldpars <- par(mai = c(sheight, 0, mheight, 0))
    on.exit(par(oldpars), add = TRUE)
    

    A workaround

    One workaround is to construct a three panel layout in which the left and right panels are just there to provide a bit of buffering space. Turn off clipping, plot your graph object in the middle panel, and it then seems to work:

    layout(matrix(1:3, nrow=1), widths=c(1,5,1))
    par(xpd=NA)                    ## turn off all clipping
    plot.new()                     ## blank plot in Panel 1
    plot(g1, edgeAttrs = eAttrs)   ## graph in Panel 2
    plot.new()                     ## blank plot in Panel 3
    

    enter image description here