使用 Plotly 添加垂直/水平参考线

Adding a Vertical / Horizontal Reference Line using Plotly

我正在使用比例条形图,我想在特定的 X 值处绘制一条垂直线。我更愿意使用 plotly 包来完成此操作,但这似乎并不容易完成。

Horizontal/Vertical Line in plotly 找到的解决方案似乎无法完成工作。

我在下面提供了一些示例代码,可用于在 X = 3 处绘制垂直线。

library(plotly)
library(ggplot2)

plot_ly(diamonds[1:1000, ], x = ~x, y = ~cut, color = ~color) %>% add_bars()

对于此事,我将不胜感激。

我在 Zappos Engineering here 的 plotly 中找到了一些关于线条的信息。 -0.5 到 4.5 的范围是因为提供的数据中有五个类别,每个类别都以一个整数为中心。 y 范围创建线,而 x 常数(3)保持线垂直。

p <- plot_ly(diamonds[1:1000, ], x = ~x, y = ~cut, color = ~color) %>% add_bars()
p <- layout(p, shapes = list(type = "line", fillcolor = "red",
                             line = list(color = "red"),
                             opacity = 1,
                             x0 = 3, x1 = 3, xref = 'x', 
                             y0 = -0.5, y1 = 4.5, yref = 'y'))