1

i haven't found the answer to the following question when googling:

Question: How do find the axis limits of an element created by plot_ly (it must be hidden in there, somewhere, or?)?

Background: I'm using plot_ly to create a lot of different timelines and I'm also updating them regularly with new data coming in.
fig <- plot_ly(....)

Now I want to mark several milestones in the plots as vertical lines. So far I haven't found how to "abline" in plot_ly - figures without giving the max x-value.

As I'm updating frequently, the maximum in the plot might change with new data. I would like to avoid runing max on all the several different timelines in the plot for several reasons.

Thanks a lot!

Code example:

fig <- plot_ly(df_timeseries, x = ~date)

fig <- fig %>% add_trace(y = ~count_mice, name = "Counts of mice",
                     mode = 'lines+markers' )

fig <- fig %>% add_trace(y = ~count_cats, name = "Counts of cats",
                     mode = 'lines+markers' )

fig <- fig %>% layout(title = "<b>Count of specimen</b>",
      yaxis = list (title = "count"),
      xaxis = list (title = "date"))

# and here is the problem. I want to add a vertical line, without having to specify y1
fig.update_layout(shapes=[
dict(
  type= 'line',
  yref= 'paper', y0= 0, y1= 100,
  xref= 'x', x0= as.Date("2020-08-04"), x1= as.Date("2020-08-04")
)

fig
Yanine
  • 21
  • 4
  • Can you add a reproducible example? I'm not following what you are trying to find out. Maybe it's just me. Do you want to know how to set coordinate limits? Or add vertical lines? Or both? – hmhensen Oct 29 '20 at 19:36
  • Dear @hmhensen, thanks for your reply. Yes, I simply want to add vertical lines without having to specify the maximum. I need to change the plots repeatedly, based on wishes of the customer. And new data comes in regularly. Therefore I dont want to run "max" over all columns I'm plotting, as I then have to remeber to change the max, too. It is error prone and I consider this bad practice. – Yanine Nov 19 '20 at 10:49

1 Answers1

0

I found the solution:

Notice how we've hard coded the extent of these lines in data coordinates; so when zooming and panning the plot, the line will be "clipped" at those values. If you don't want these lines to be clipped, use a line shape with xref/yref set to paper (this puts the graph region on a 0-1 scale, rather than on the x/y data scale)

source: Horizontal/Vertical Line in plotly

This way I don't need to find the maximum, I can simply draw the line and it will be fitted to the plot limits automatically.

Yanine
  • 21
  • 4