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
Suppose these are two nodes.
tempNodes <- data.frame ('x' = c(10, 40), 'y' = c(10, 30) )
And these are the endpoints from directed lines (one in each direction).
data <- data.frame('x' = c(10,40), 'y' = c(10,30), 'xend' = c(40,10), 'yend' = c(30,10))
Then I wrap up the math borrowed from the 'geom_segment_plus' code and get this.
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)
}
So if I assign that to 'temp' like this:
temp <- segementsDf(data, 2.5, 2.5, 2)
Then I can run it in 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))
And I get this (without arrows for now but pretty close... I can tinker with the offset and end values).
Super clunky (and I'll clean it up a bit to match workflow) but for now it solves the problem.