简体   繁体   English

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

[英]How to add annotations horizontal or vertical reference line in plotly?

I am using plotly for R. output using shiny if that matters. 如果这很重要,我正在使用闪亮的R. I want to add annotations to horizontal or vertical reference line created by layout(shapes = ) . 我想向由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 基本上, yref = "paper"允许您指定始终相对于图的位置, y=1表示图的顶部, y=0表示图的底部

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 基本上,yref =“ paper”允许您指定始终相对于图的位置,y = 1表示图的顶部,y = 0表示图的底部

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM