如何在plotly中添加注释水平或垂直参考线?

Wei*_* Gu 4 r plotly

如果重要的话,我正在使用闪亮的 R. 输出。我想向由layout(shapes = ). 它应该能够动态改变位置,以便无论图形放大或缩小如何,它都保持可见。 近似示例

到目前为止,我只能找到固定位置的文本。我不介意它是动态位于轴上还是指向参考线的箭头。

%编辑

我找到了解决问题的方法:

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))
Run Code Online (Sandbox Code Playgroud)

基本上,yref = "paper"允许您指定一个始终相对于图的位置,y=1指的是图的顶部和图y=0的底部

Wei*_* Gu 6

我找到了解决问题的方法:

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))
Run Code Online (Sandbox Code Playgroud)

基本上, yref = "paper" 允许您指定始终相对于绘图的位置,y=1 表示绘图的顶部,y=0 表示绘图的底部