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')))
Run Code Online (Sandbox Code Playgroud)
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.
假设这是两个节点。
tempNodes <- data.frame ('x' = c(10, 40), 'y' = c(10, 30) )
Run Code Online (Sandbox Code Playgroud)
这些是有向端点(每个方向一个)。
data <- data.frame('x' = c(10,40), 'y' = c(10,30), 'xend' = c(40,10), 'yend' = c(30,10))
Run Code Online (Sandbox Code Playgroud)
然后,我总结了从“ geom_segment_plus”代码中借用的数学并得到了这个。
segementsDf <- function(data, shorten.start, shorten.end, offset){
data$dx = data$xend - data$x
data$dy = data$yend - data$y
data$dist = sqrt( data$dx^2 + data$dy^2 )
data$px = data$dx/data$dist
data$py = data$dy/data$dist
data$x = data$x + data$px * shorten.start
data$y = data$y + data$py * shorten.start
data$xend = data$xend - data$px * shorten.end
data$yend = data$yend - data$py * shorten.end
data$x = data$x - data$py * offset
data$xend = data$xend - data$py * offset
data$y = data$y + data$px * offset
data$yend = data$yend + data$px * offset
return(data)
}
Run Code Online (Sandbox Code Playgroud)
因此,如果我将其分配给“ temp”,如下所示:
temp <- segementsDf(data, 2.5, 2.5, 2)
Run Code Online (Sandbox Code Playgroud)
然后我可以在ggplot中运行它:
ggplot(tempNodes, aes(x = x, y = y)) + geom_point(size = 12) + xlim(0,50) +
ylim(0,50) + geom_segment(data = temp, aes(x = x, xend = xend, y = y, yend = yend))
Run Code Online (Sandbox Code Playgroud)
我知道了(现在没有箭头,但是很近...我可以修改offset和end值)。
超级笨拙(我将对其进行清理以适应工作流程),但现在它可以解决问题。