使用 Plotly 添加垂直/水平参考线 [英] Adding a Vertical / Horizontal Reference Line using Plotly

查看:105
本文介绍了使用 Plotly 添加垂直/水平参考线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working with a proportional bar chart and I'd like to draw a vertical line at a particular X value. I'd prefer to accomplish this using the plotly package, but it doesn't seem to be easily done.

情节中的水平/垂直线中找到的解决方案似乎没有把事做好.

The solution found at Horizontal/Vertical Line in plotly doesn't seem to get the job done.

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

I've provided some sample code below that could be used to draw the vertical line at 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.

推荐答案

我从 Zappos Engineering 此处.-0.5 到 4.5 的范围是因为提供的数据中有五个类别,每个类别都以一个整数为中心.y 范围创建线,而 x 常量(在 3 处)保持线垂直.

I found some information about lines in plotly from Zappos Engineering here. The range -0.5 to 4.5 is because there are five categories in the data provided, each centered on a whole number. The y range creates the line, while the x constant (at 3) keeps the line vertical.

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'))

这篇关于使用 Plotly 添加垂直/水平参考线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆