r - 更改 geom_spoke 中箭头的方向

标签 r ggplot2

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), radius = 0.7, arrow=arrow(length = unit(0.2,"cm")))

enter image description here

最佳答案

geom_spoke 中的arrow 指的是grid::arrow,它有ends 参数:

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

因为您没有显式设置 ends 参数,所以使用了默认值 "last",即段末尾的箭头。

一个小例子:

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

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)

箭头在段的开始,设置ends = "first":

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

enter image description here

关于r - 更改 geom_spoke 中箭头的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39173710/

相关文章:

r - 光栅包绘图函数的命名空间问题?

r - 计算 ggplot2 stat_binhex 中 bin 的百分比

r - "zoom"/"scale"与 coord_polar()

r - 使 ggplot 看起来像 R 中的原生图一样漂亮

r - 如何控制 write.table() 输出中的小数位数?

r - 将嵌套列表转换为列表

r - facet_wrap的百分比直方图

r - 从 R/DBI/ODBC 调用 SQL 服务器上的存储过程时避免生成动态 SQL

r - 将 R ggplot 中直方图中的 y 轴标准化为比例

r - 在 ggplot 中制作圆形线端 - 在情节和图例中