如何在GGPLOT2传奇中向后和向前的方向显示箭头?
我有一个 dataFrame
,上面有箭头的坐标:
arrows = data.frame(direction = factor(c("forward", "backward", "backward")),
x = c(0, 0, 0),
xend = c(1, 1, 1),
y = c(1, 1.2, 1.1),
yend = c(1, 1.2, 1.1))
我想使用此数据绘制箭头 - 有些朝向前进方向,反之亦然。
到目前为止,我尝试了
library(ggplot2)
ggplot() +
geom_segment(data = arrows,
aes(x, y, xend = xend, yend = yend, col = direction),
arrow = arrow(length = unit(0.3, "cm"), type = "closed", ends = c("last", "first")))
:
我想在此图中解决两个问题:
-
我如何确保
ggplot
'理解' “前进”和“向后”,所以它给了他们正确的传奇吗? -
如何更改图例,以便它以向前向前的箭头和向后的向后箭头显示向前的方向?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许有两个传说,一个使用自定义字形?
Perhaps with two legends, one using a custom glyph?
如何将“方向”映射到“第一个”和“最后”,例如,
我还使用
scale_colour_manual
来控制哪个方向是向前且向后的。关于传说中的自定义字形的另一个问题 - - 这应该允许不同的箭头方向,但可能不值得。
How about mapping the "direction" to "first" and "last" directly, e.g.
I'd also used a
scale_colour_manual
to control which direction is forward and which backward.Another question on SO asked about custom glyphs in legends - show filled arrow in legend ggplot - This should allow the different arrow direction, but might not be worth the effort.
需要为每个箭头设置端的参数,而您的末端仅是两个长度,因此最后一个箭头将是错误的方向,因为它使用的是“最后”的默认值,但是由于箭头向后移动,它应该是“第一个”
The ends argument needs to be set for each arrow, yours is only length two so that last arrow will be the wrong direction since it is using the default which is "last" but since the arrow goes backwards it should be "first"
您可以根据方向列以X/XEND交换列值。
在2022-06-28上创建的 reprex软件包(v2.0.1)
You could swap the column values in x/xend based on the direction column.
Created on 2022-06-28 by the reprex package (v2.0.1)