在ggplot2中很好地安排点之间的箭头 [英] Arranging arrows between points nicely in ggplot2

查看:100
本文介绍了在ggplot2中很好地安排点之间的箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(注意 - 这与。



我沿用了代码这个:

  ggplot(points,aes(x = x,y = y))+ geom_point(aes(size = size ),color =red,shape = 1)+ 
scale_size_continuous(range = c(4,20))+
geom_segment_plus(data = trans [trans $ amount> 0.3,],
aes(x = x.from,y = y.from,xend = x.to,yend = y.to),
lineend =round,arrow = arrow(length = unit(0.15,i ),
alpha = 0.5,size = 1.3,
offset = 0.01,shorten.start = 0.03,shorten.end = 0.03)
pre>

这绝对不是完美的,但它起作用 - 您可以在这里看到一个双箭头到左下角点。



offset,shorten.start和shorten.end是添加的aes元素。他们可以设置为数据点,但我还没有想出如何正确扩展它们。




(note - this is the same piece of work as using multiple size scales in a ggplot, but I'm asking a different question)

I'm trying to construct a plot which shows transitions from one class to another. I want to have circles representing each class, and arrows from one class to another representing transitions.

I'm using geom_segment with arrow() to draw the arrows. Is there any way to:

  • make the arrows stop before they reach the circles
  • adjust the position so that if there is an arrow in both directions, they are "dodged" rather than overlapping.

I couldn't get position="dodge" to do anything useful here.

As an example:

library(ggplot2)
points <- data.frame( x=runif(10), y=runif(10),class=1:10, size=runif(10,min=1000,max=100000) )
trans <- data.frame( from=rep(1:10,times=10), to=rep(1:10,each=10), amount=runif(100)^3 )
trans <- merge( trans, points, by.x="from", by.y="class" )
trans <- merge( trans, points, by.x="to", by.y="class", suffixes=c(".to",".from") )
ggplot( points, aes( x=x, y=y ) ) + geom_point(aes(size=size),color="red",shape=1) + 
    scale_size_continuous(range=c(4,20)) +
    geom_segment( data=trans[trans$amount>0.6,], aes( x=x.from, y=y.from, xend=x.to, yend=y.to ),lineend="round",arrow=arrow(),alpha=0.5, size=0.3)

解决方案

I've put together a simple extension of geom_segment, which allows specification of

  • shortening at the start and end of the lines
  • an amount to offset lines which share a reversed source and destination

It's up on pastebin here: geom_segment_plus.

I used code along the lines of this:

ggplot( points, aes( x=x, y=y ) ) + geom_point(aes(size=size),color="red",shape=1) +
    scale_size_continuous(range=c(4,20)) + 
    geom_segment_plus( data=trans[trans$amount>0.3,], 
        aes( x=x.from, y=y.from, xend=x.to, yend=y.to ),
        lineend="round",arrow=arrow(length=unit(0.15, "inches")),
        alpha=0.5, size=1.3, 
        offset=0.01, shorten.start=0.03, shorten.end=0.03)

It's definitely not perfect, but it works - you can see a double arrow going to the bottom left point here.

offset, shorten.start and shorten.end are the aes elements added. They can be set to data points, but I haven't figured out how to scale them properly.

这篇关于在ggplot2中很好地安排点之间的箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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