如何在geom_segment中获得T型箭头?

问题描述 投票:0回答:1

我需要使用geom_crossbar在ggplot上手动添加误差线。

我已经尝试过“ geom_arrow”,但是我找不到如何更改箭头的大小或将箭头的角度更改为90度的方法(就像我可以在图中使用功能箭头一样)。我得到的最接近的是“ geom_segment”,可以选择箭头的长度,但仍然无法更改箭头的角度。

以下是我想要获得的示例:enter image description here

这里是带有geom_segment的代码:

xaxis = c(5,6,7,8)
yaxis1 = c(3,3,2,1)
yaxis2 = c(6,5,3,3)
df = data.frame(cbind(xaxis,yaxis1,yaxis2))
ggplot(df) +
  geom_crossbar(aes(ymin=yaxis1, ymax=yaxis2, 
                    x=xaxis, y=yaxis1),
                fill = alpha("black",0.5), fatten=0) +
  geom_segment(mapping=aes(x=xaxis, y=yaxis1-0.4, xend=xaxis, yend=yaxis1+0.4),
               color="black",
               arrow=arrow(length = unit(0.25, "cm"), ends="both"))

任何帮助表示赞赏!

r ggplot2 arrows
1个回答
0
投票

ggplot中,您可以使用geom_errorbar向条形图中添加误差线,然后使用参数width设置宽度:

library(ggplot2)
ggplot(df) +
  geom_crossbar(aes(ymin=yaxis1, ymax=yaxis2, 
                    x=xaxis, y=yaxis1),
                fill = alpha("black",0.5), fatten=0) +
  geom_errorbar(mapping=aes(x=xaxis, ymin=yaxis1-0.4, ymax=yaxis1+0.4),
               color="black", width = 0.25)

enter image description here

这是您要寻找的吗?

© www.soinside.com 2019 - 2024. All rights reserved.