1

I know how to extract axis limits after making a plot with base R functions.

plot(Sepal.Length ~ Sepal.Width, iris)
(Horizontal_Axis_Limits <- par('usr')[1:2])
(Vertical_Axis_Limits <- par('usr')[3:4])

There's a good answer here for ggplot2 figures, but is there a way to do this same thing when using other packages such as the dygraphs package? I know about the valueRange argument to the dygraphs::dyAxis() function, but I'm not sure if I can somehow use that to extract the axis limits after the graph has already been created. Here's a reproducible example.

Time <- seq.POSIXt(as.POSIXct('2022-1-1 00:00:00'), as.POSIXct('2022-3-1 00:00:00'), by = '1 h')
Response <- sin(seq_len(length(Time)) / 100) + rnorm(length(Time), 0, 0.25)
Data_Frame <- data.frame(Time = Time, Response = Response)
Data <- xts(x = Data_Frame$Response, order.by = Data_Frame$Time)
(Figure_1 <- dygraphs::dygraph(Data))

Here's a plotly reproducible example as well. There's a Stack Overflow answer here that gives an answer to this question for python, but it doesn't provide anything for R.

(Figure_2 <- plotly::plot_ly(ggplot2::economics, x = ~date, y = ~pop))

Thank you!

David Moore
  • 670
  • 3
  • 15
  • I don't know if there's a way to do it with `plotly` in R - but if there isn't you can always access the underlying javascript object with `htmlwidgets`. See [this](https://stackoverflow.com/questions/74668844/show-mouse-hover-info-as-annotation-in-a-plotly-r-boxplot) answer for an example of the approach. I am not familiar with `dygraphs` but I imagine it would be harder as plots render using canvas rather than svg. – SamR Feb 03 '23 at 21:38
  • That's interesting - thanks. I've never written JavaScript code so I wouldn't be able to do that. I'd certainly upvote someone's answer if they wanted to give it a shot! – David Moore Feb 03 '23 at 21:46

0 Answers0