5

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:

enter image description here

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.

Community
  • 1
  • 1
noLongerRandom
  • 521
  • 1
  • 5
  • 17
  • The [`geomnet` package](https://cran.r-project.org/web/packages/geomnet/index.html) adds a network visualization geom to ggplot. I haven't used it but it might be what you're looking for: Here's a [presentation with some examples](http://sctyner.github.io/JSM2015TynerHofmann.html#/). – eipi10 Mar 10 '16 at 02:24
  • In the original post I noted that I had looked at both 'geomnet' and 'GGally'. I went through the documentation for both. I didn't find anything that handles this with the caveat that it's possible I missed something. – noLongerRandom Mar 10 '16 at 15:36
  • 1
    So, not the best solution but, taking the math at the heart of the transformation of 'geom_segment_plus' I can build a function to add some columns to a data.frame and with those variables get what I'm after (at least on a test run of two points). I looked at Hadley's brief tutorial on extending ggplot2 and modifying a geom, but that's beyond my reach right now. Will leave this up in case someone can get the referenced geom working. Would make work flow easier/better, but for now at least I can proceed, however clunky. – noLongerRandom Mar 10 '16 at 17:52
  • 1
    If you come up with a solution that works for you (even if clunky), please post it as an answer. – eipi10 Mar 10 '16 at 18:01

2 Answers2

6

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).

enter image description here

Super clunky (and I'll clean it up a bit to match workflow) but for now it solves the problem.

noLongerRandom
  • 521
  • 1
  • 5
  • 17
2

You can use geom_pointpath inside annotate()from ggh4x package for it.

You still need to dodge it vertically and horizontally, but it's way easier than other answers.

library(dplyr)
library(ggh4x)
library(ggplot2)

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) + 
  annotate(
    ggh4x::GeomPointPath,
    x = c(10,40)-0.2, y = c(10,30)+1,
    mult = 2,
    shape = NA,
    arrow = arrow(length = unit(.3, 'cm'))
  ) + 
  annotate(
    ggh4x::GeomPointPath,
    x = c(40,10)+0.2, y = c(30,10)-1,
    mult = 2,
    shape = NA,
    arrow = arrow(length = unit(.3, 'cm'))
  )

Created on 2022-11-15 by the reprex package (v2.0.1)

Bruno Mioto
  • 382
  • 1
  • 10