在ggplot中偏移geom_segment [英] Offset geom_segment in ggplot

查看:920
本文介绍了在ggplot中偏移geom_segment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot中建立一个网络图。两件事:1)我需要把节点放在特定的(x,y)值。这不是问题。 2)网络图是直接的,但我需要能够显示从节点B到节点A的差异,而不是从节点A到节点B.

这就是后面我遇到的麻烦。基本上我需要抵消节点之间并行运行的两条线。最终,这些行的权重将被映射到某些内容,但大致如下所示: iSixB.jpgrel =nofollow noreferrer>



但是这个代码全部由手工生成(粘贴在下面以供参考)。我试图获得在ggplot中完成的偏移量,其中我已经有用于节点位置的(x,y)对,以及(x,y)之间的连接的边缘列表。

  offsetDf < -  data.frame('x'= c(10,40),'y'= c(10 ,'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')))

我看了一下GGally和geomnet,但是看起来都没有处理这个问题。我发现有人构建了一个小型geom来完成这个任务 - 它具有偏移和缩短段末端的输入(所以它们不会一直走到节点)。它在这个SO页面上(滚动到底部):



超级笨重(我会将其清理一下以匹配工作流程),但现在它解决了 问题。

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.

解决方案

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.

这篇关于在ggplot中偏移geom_segment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆