5

I am using plotly for R. output using shiny if that matters. I want to add annotations to horizontal or vertical reference line created by layout(shapes = ). It should be able to dynamically change position so that it stays visible no matter how plotly graph is zoomed in or out. Approximate Example

So far I can only find fixed location text. I don't mind if it is dynamically located on axis or arrow pointing to reference lines.

%Edited

I found the sulotion to the problem:

dt <- data.table(x = 1:10, y = rnorm(10))    
annotation <- list(yref = 'paper', xref = "x", y = 0, x = 2, text = "annotation")
plot_ly(type = "scatter", mode = "lines") %>% add_trace(x = dt$x, y = dt$y, mode = "lines") %>% layout(annotations= list(annotation))

Basically, yref = "paper" allow you to specify a position that is always relative to the plot, y=1 refers to the top of plot and y=0 refers to the bottom of the plot

Weiwen Gu
  • 205
  • 3
  • 13
  • 3
    Welcome to StackOverflow :). Note that questions will be answered far more quickly, and attract upvotes and reputation, if they are accompanied with a stand-alone and minimal example that illustrates the question. – Mike Wise Dec 21 '16 at 16:33
  • Thank you edited. My apologies I am not able to directly post image yet. – Weiwen Gu Dec 21 '16 at 22:16
  • 1
    Try adding a geom_hline() layer to your plot? It would be easier to understand what you need if you could post a minimal working example of the code you're using, and then we can make suggestions to modify it to get the effect you want. – Rose Hartman Dec 21 '16 at 22:26

1 Answers1

7

I found the sulotion to the problem:

dt <- data.table(x = 1:10, y = rnorm(10))    
annotation <- list(yref = 'paper', xref = "x", y = 0, x = 2, text = "annotation")
plot_ly(type = "scatter", mode = "lines") %>% add_trace(x = dt$x, y = dt$y, mode = "lines") %>% layout(annotations= list(annotation))

Basically, yref = "paper" allow you to specify a position that is always relative to the plot, y=1 refers to the top of plot and y=0 refers to the bottom of the plot

Weiwen Gu
  • 205
  • 3
  • 13