简体   繁体   English

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

[英]Adding a Vertical / Horizontal Reference Line using Plotly

I'm working with a proportional bar chart and I'd like to draw a vertical line at a particular X value. 我正在使用比例条形图,我想在特定的X值处绘制一条垂直线。 I'd prefer to accomplish this using the plotly package, but it doesn't seem to be easily done. 我更愿意使用plotly包来完成此操作,但是似乎并不容易做到。

The solution found at Horizontal/Vertical Line in plotly doesn't seem to get the job done. 水平/垂直线中找到的解决方案似乎无法完成工作。

I've provided some sample code below that could be used to draw the vertical line at X = 3. 我在下面提供了一些示例代码,可用于在X = 3处绘制垂直线。

library(plotly)
library(ggplot2)

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

I'd appreciate any help in this matter. 我很乐意为您提供帮助。

I found some information about lines in plotly from Zappos Engineering here . 我发现了大约从Zappos的工程plotly线的一些信息在这里 The range -0.5 to 4.5 is because there are five categories in the data provided, each centered on a whole number. -0.5到4.5的范围是因为提供的数据中有五个类别,每个类别都以整数为中心。 The y range creates the line, while the x constant (at 3) keeps the line vertical. 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'))

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

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