plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

ggplotly can't change axis range with dynamic ticks

Open Actal opened this issue 5 years ago • 4 comments

Hi, I tried to make a plot with a zoom and with dynamic ticks. But after the rendering there is no zoom.

library(plotly)
df = data.frame(x=rnorm(100), y=rnorm(100))
g <- ggplot(df, aes(x=x, y=y)) + geom_point()
ggplotly(g, dynamicTicks = TRUE) %>% layout(xaxis = list(range = c(-0.5,0.5)))

Maybe this is incompatible or there is an other way to do it? Thanks

Actal avatar Sep 07 '20 15:09 Actal

That's strange, can you include a screenshot of the modebar?

cpsievert avatar Sep 08 '20 14:09 cpsievert

image

Actal avatar Sep 09 '20 07:09 Actal

I faced this problem today as well. I've realized that dynamicTicks argument sets autorange to be true - thus the provided range is ignored. To leverage an explicit range, one has to explicitly disable autorange. This worked for me:

library(plotly)
library(ggplot2)

p <- ggplot(
  data = mtcars,
  aes(x = wt, y = mpg, color = factor(cyl))
) + geom_point()

ggplotly(p, dynamicTicks = TRUE) |>
  layout(
    yaxis = list(
      range = c(0, 50),
      autorange = FALSE
    )
  )

Gotfrid avatar Dec 09 '24 14:12 Gotfrid

Thank you @Gotfrid! I was experiencing the same issue with my plots made with ggplotly and your solution works perfectly on all of them.

gaudasyl avatar Jan 10 '25 15:01 gaudasyl