I try to combine a horizontal bar chart with a vertical line across the whole plot area using plotly. But i cant figure out, how to extend the line from bottom to top.
I know how to do it with ggplot, but I explicitely want to do it with plotyl and without ggplot-conversion.
There are a couple of solutions with plotly on stackoverflow and other pages, but all solutions I found are dealing with two continuos axis.
Here is an example code I have so far:
library(plotly)
library(tidyverse)
df <- tribble(
~region , ~status ,
'Reg 1' , 10000 ,
'Reg 2' , 20000 ,
'Reg 3' , 15000
)
target <- 17000
plot_ly(df , orientation = 'h') %>%
add_bars(x = ~status , y = ~region ,
text = ~status , textposition = 'inside') %>%
add_trace(x = target , y = ~region ,
type = 'scatter' , mode = 'lines' ,
line = list(color = 'darkred')) %>%
layout(showlegend = FALSE ,
annotations = list(text = paste('Target:\n' , target) ,
xref = 'x' , yref = 'paper' ,
x = target , y = 0.90 ,
xanchor = 'left' , yanchor = 'top' ,
showarrow = FALSE ,
font = list(color = 'darkred')))
I hope, someone can give me a hint, how to extend the red line from bottom to the top.