更改geom_spoke中箭头的方向 [英] Change direction of arrows in geom_spoke

查看:193
本文介绍了更改geom_spoke中箭头的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

geom_spoke()中有没有办法让箭头指向给定数据的方向?如果我有位置(x,y)和角度的数据,使用 geom_spoke 似乎是理想的选择,但我希望能够选择在哪个方向箭头应指向(远离坐标或指向它们;我想要后者)。



在本例中,我添加了 geom_point()来说明数据的位置。半径是随机选取的,所以我想要箭头指向现在的点(当然不是点):

  set.seed(1)
df < - expand.grid(x = 1:10,y = 1:10)
df $ angle < - runif(100,0,2 * pi)

ggplot(df,aes(x,y))+
geom_point()+
coord_equal()+
geom_spoke(aes(angle = angle) ,半径= 0.7,箭头=箭头(长度=单位(0.2,厘米)))

解决方案

箭头 geom_spoke 引用 grid:arrow ,其中结束参数:


last,firstboth,指示要绘制箭头的线的哪一端。


因为您没有明确设置 ends 参数,所以last的默认值

一个小例子:

  p <-ggplot(df,aes(x = x,y = y,angle = angle))+ 
geom_point()

结束的默认值:



<$ p $ b $ lt; code>#省略ends参数,就像在你的尝试中一样:
p + geom_spoke(arrow = arrow(length = unit(0.2,cm)),radius = 0.7)

#或者明确写出ends参数:
p + geom_spoke(arrow = arrow(ends =last,length = unit(0.2,cm)),radius = 0.7)

在分段开始的箭头处,设置 ends =first
$ p $ p + geom_spoke(arrow = arrow(ends =first),radius = 0.7)


Is there a way in geom_spoke() to get arrows to point to the direction of the given data? If I have data with a location (x,y) and an angle, it seems ideal to me to use geom_spoke, but I would expect to be able to choose in which direction the arrows should point (away from the coordinates or pointing to them; I want the latter).

In this example, I added geom_point() to illustrate the location of my data; the radius is chosen randomly, so I want the arrow heads where the points are now (and instead of the points, of course):

set.seed(1)
df       <- expand.grid(x = 1:10, y=1:10)
df$angle <- runif(100, 0, 2*pi)

ggplot(df, aes(x, y)) +
  geom_point()        +
  coord_equal()       +
  geom_spoke(aes(angle = angle), radius = 0.7, arrow=arrow(length = unit(0.2,"cm")))

解决方案

The arrow in geom_spoke refers to grid::arrow, which has the ends argument:

One of "last", "first", or "both", indicating which ends of the line to draw arrow heads.

Because you didn't explicitly set the ends argument, the default value of "last" was used, i.e. arrow at the end of the segment.

A small example:

p <- ggplot(df, aes(x = x, y = y, angle = angle)) +
  geom_point() 

Default value of ends:

# omitting the ends argument, like in your attempt:
p + geom_spoke(arrow = arrow(length = unit(0.2, "cm")), radius = 0.7)

# or writing out the ends argument explicitly:
p + geom_spoke(arrow = arrow(ends = "last", length = unit(0.2, "cm")), radius = 0.7)

Arrow at the start of the segment, set ends = "first":

p + geom_spoke(arrow = arrow(ends = "first"), radius = 0.7)

这篇关于更改geom_spoke中箭头的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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