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")))
最佳答案
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)
关于r - 更改 geom_spoke 中箭头的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39173710/