简体   繁体   English

向分类轴上的绘图添加垂直参考线

[英]Adding a vertical reference line to a plot on a categorical axis

In the plot below, I would like to add a custom error bar extending to the right of the bar.在下图中,我想添加一个延伸到条形右侧的自定义误差条。 I'm halfway there, as shown below, in other words, I have the horizontal line, but I can't figure out how to add the vertical bar to the end of the skinny line as shown in green below.我已经到了一半,如下图所示,换句话说,我有水平线,但我不知道如何将垂直条添加到细线的末端,如下图绿色所示。 Does anyone have any ideas on how to accomplish this?有没有人对如何实现这一目标有任何想法?

library(magrittr)
library(plotly)

df <- data.frame(grp = c("A", "B", "C", "D", "E") %>% as.factor,
           val = c(11:15)) %>% 
df %>%
  plot_ly(x = ~val,
          y = ~grp,
          type = "bar") %>% 
  add_segments(xend = 20,
               yend = ~grp)

在此处输入图片说明

You may add a marker to the end of the segment.您可以在段的末尾添加一个标记。
In addition you can group the legends of segement and marker together, so there are hidden/shown at the same time when toggling legend items.此外,您可以将段和标记的图例组合在一起,因此在切换图例项时会同时隐藏/显示。

Code代码

 df %>%
  plot_ly(x = ~val,
          y = ~grp,
          type = "bar") %>% 
  add_segments(xend = 20,
               yend = ~grp,
               legendgroup = "g1") %>%
  add_markers(x = 20,
              # style marker
              marker = list(
                symbol = "line-ns-open",
                size = 15,
                line = list(
                  width = 5
                )),
              # combine legends
              legendgroup = "g1",
              showlegend = F)
  

Plot阴谋
在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM