Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
743 views
in Technique[技术] by (71.8m points)

r - Offset geom_segment in ggplot

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...