6

I would like to zoom in on the y-axis on a plotly plot that uses rangeslider.

A reproducible example:

library(ggplot2)
library(plotly)
p <- ggplot(faithful, aes(x = waiting)) +
    geom_histogram(bins = 30)
p <- ggplotly(p)
p <- rangeslider(p)
p

The way I can zoom is the following:

However, I would like to be able to also zoom like this (which is done by not adding a rangeslider):

I assume this can be done by using something along the lines of

p <- layout(p, dragmode = "zoom")

but I haven't been able to make this work.

M--
  • 25,431
  • 8
  • 61
  • 93
NiklasvMoers
  • 309
  • 2
  • 13
  • 1
    ```p <- rangeslider(p, start = min(floor(p$x$data[[1]]$x))-1, end = max(ceiling(p$x$data[[1]]$x))+1) %>% layout(p, yaxis = list(fixedrange = FALSE))``` this works too. Notice that you need to specify `start` and `end` if you are setting up `rangeslider` outside of `layout`. – M-- Jul 14 '20 at 18:50

1 Answers1

6

In reference to this GitHub issue, it can be done by setting fixedrange to FALSE for yaxis.

library(ggplot2)
library(plotly)

ggplot(faithful, aes(x = waiting)) +
  geom_histogram(bins = 30) -> p

ggplotly(p) %>% 
  layout(xaxis = list(rangeslider = list()),
         yaxis = list(fixedrange = FALSE)) -> p

M--
  • 25,431
  • 8
  • 61
  • 93
  • 2
    This does exactly what I want and it also fixes the issue of the rangeslider having weird default start and end values (which I wasn't able to fix setting ```end = max(data$x)```). Thanks! – NiklasvMoers Jul 15 '20 at 08:42
  • 1
    Amazingly simple answer, this will override the Plotly defaults when using rangeslider, even for subplots. – JJ Fantini Jun 29 '23 at 15:54