kputschko
kputschko

Reputation: 806

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. 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.

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.

Upvotes: 1

Views: 1854

Answers (1)

gleasocd
gleasocd

Reputation: 103

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

Upvotes: 3

Related Questions