Reputation: 2126
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.
Upvotes: 1
Views: 1874
Reputation: 10825
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")
# 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="-")
Upvotes: 1
Reputation: 7928
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.
Upvotes: 1