I'm trying to build a network plot in ggplot. Two things: 1) I need to put the nodes at specific (x, y) values. This isn't a problem. 2) The network plot is directed but I need to be able to show differences in going from, say, Node B to Node A than from Node A to Node B.
It's that latter bit I'm having trouble with. Basically I need to offset two lines running parallel between nodes. Ultimately the lines' weights will be mapped to something, but roughly it looks like this:
But this code is all generated by hand (pasted below for reference). I'm trying to get the offset done in ggplot where I already have the (x, y) pairs for the node positions, as well as an edge list for the connections between the (x,y).
offsetDf <- data.frame('x' = c(10, 40), 'y' = c(10, 30), 'startX' = c(13, 36.5), 'startY' = c(11, 29), 'endX' = c(37.5, 12), 'endY' = c(27, 13) )
ggplot(offsetDf, aes(x = x, y = y)) +
geom_point(size = 13) +
xlim(0,50) + ylim(0,50) +
geom_segment(aes(x = startX, y = startY, xend = endX, yend = endY),
arrow = arrow(length = unit(.3, 'cm')))
I looked at both GGally and geomnet but neither looks like it has anything that handles this. I found someone who built a little geom to do exactly this — it has inputs for both offset and shortening the ends of the segments (so they don't go all the way to the node). It's on this SO page here (scroll all the way to the bottom): geom_segment_plus on SO
But it no longer works. When I try to use it I get an error reading:
Error in eval(expr, envir, enclos) : could not find function "eval"
Which, doing a little googling, seems to have something to do with the last major overhaul of ggplot (and I'm not adept enough as a coder to go under the hood and figure out exactly how to fix it). There will be hundreds of plot with 10-20 nodes each, so trial and error by hand isn't really gonna happen. Any help is appreciated.
See Question&Answers more detail:
os