1
votes

Consider a plot of a very simple undirected graph produced with igraph.

library(igraph)

edges = matrix(c(1,2), nrow=1, ncol=2)
g = graph.edgelist(edges, directed=FALSE)
set.seed(42)
plot(g, edge.width=2, vertex.size=30, edge.color='black', main='')

There is a small gap between the edge and vertex 2, but not between the edge and vertex 1. Is there a way of removing that gap?

The graph I am really working with is larger and edges are a lot shorter. While the gap goes almost unnoticed in the above example, it is confusing in my case.

2
This works for me fine for me. I do know that what you describe tends to happen if you try to plot in a non-square plotting area but I don't remember how you actually do that. Related, you could try my package library("qgraph");qgraph(edges) in which this behavior should not occur.Sacha Epskamp
I see the problem under Windows. The gap increases in size as the window is resized. It is a very small gap in this case, but I do appreciate your efforts for perfection. Could you use layout() (the igraph function) to return coordinates of vertices, then plot your own graph from scratch. Plot edges first then vertices on top.digitalmaps
Might be a bug. Btw. if you resize the plot, then it is best to redraw it, because the rescaling might magnify rounding errors. It is best to report igraph bugs at bugs.launchpad.net/igraph.Gabor Csardi

2 Answers

1
votes

This is indeed a bug in igraph, and it happens because igraph leaves some space there for the arrow-head, even if the arrow-head is not there. I will fix it in the next igraph version.

As a workaround, what you can do is plotting each edge twice, on top of each other. For this you need to make your graph directed and then use the edge.arrow.mode option to avoid the arrows. This works because only one end of the edge is modified by the arrow plotter. Somewhat stupid to plot your graphs this way, but I cannot find a better workaround for now. As I said, the new version (the one after 0.6.4) will not have this problem.

library(igraph)

# Zoom in on the critical region, although the gap always has the 
# same size, unless you make the plotting window bigger
g <- graph(c(1,2), directed=FALSE)
par(mar=c(0,0,0,0))
plot(g, edge.width=2, vertex.size=20, edge.color='black', main='',
     rescale=FALSE, xlim=c(0.9,1), ylim=c(0.9,1),
     layout=rbind(c(0,0), c(1,1)), vertex.color="#ffffff11")

gap

# This plot should have no gaps
g2 <- as.directed(g, mode="mutual")
par(mar=c(0,0,0,0))
plot(g2, edge.width=2, vertex.size=20, edge.color='black', main='',
     rescale=FALSE, xlim=c(0.9,1), ylim=c(0.9,1),
     layout=rbind(c(0,0), c(1,1)), vertex.color="#ffffff11",
     edge.arrow.mode="-") 

no gap

1
votes

I read the ?igraph page and saw a recommendation to use the 'good-looking anti-aliased Cairo device' ?igraph.

So, I tried

library(Cairo)
Cairo(1600, 1600, file="plot.png", type="png", bg="white")
plot(g, edge.width=2, vertex.size=30, edge.color='black', main='')
dev.off()

and,

CairoPDF("plot.pdf", 6, 6, bg="transparent")
plot(g, edge.width=2, vertex.size=30, edge.color='black', main='')
dev.off()

Both the png and pdf version look like there is no small gap.

Let me know if it works for you.