3

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?

2 Answers 2

5

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

1
  • By the way, I get an error with par(mai = c(sheight, 0, mheight, 0)) May 16, 2013 at 22:28
0

I found another solution: In my original question I changed the size of the bounding box in a laid out graph I got with agopen. Plotting the laid out graph showed no edge labels at all. I found that it is possible to pass the edge attributes from the graph object to agopen and then change the bounding box:

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")
z <- agopen(g1, "foo", edgeAttr=eAttrs)
z@boundBox@botLeft@x <- z@boundBox@botLeft@x - 400 ##left
z@boundBox@upRight@x <- z@boundBox@upRight@x + 200 ##right
plot(z)

The plot:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.