shosaco / vistime

Pretty timelines in R.
https://shosaco.github.io/vistime
GNU General Public License v3.0
168 stars 11 forks source link

restrict zooming to x-dimension #12

Closed mayeulk closed 1 year ago

mayeulk commented 4 years ago

Is it possible to restrict zooming so that one can zoom only in the x dimension? Such as with: %>% layout(yaxis=list(fixedrange=TRUE)) discussed at cf https://community.plot.ly/t/disable-interactions-in-plotly-for-r-and-ggplot2/1361 and layout.xaxis.fixedrange = True at https://github.com/plotly/plotly.js/issues/1007#issuecomment-585278919 Zooming in the y dimension gives often weird results in vistime

shosaco commented 4 years ago

Dear mayeulk,

since vistime is a plotly object, you can do anything that you can do with a plotly object, such as restriting the zooming:

library(vistime)
library(magrittr)
pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                   Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                   start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                   end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                   color = c('#cbb69d', '#603913', '#c69c6e'),
                   fontcolor = c("black", "white", "black"))

vistime(pres, events="Position", title="Presidents of the USA") %>% plotly::layout(yaxis=list(fixedrange=TRUE))

image

mayeulk commented 4 years ago

Hi Sandro, thank you very much for your help on putting %>% layout() in the right place! Below a minimal working Shiny app with this:

library(shiny)
library(plotly)
library(vistime)
library(magrittr)
pres <- data.frame(Position = rep(c("President", "Vice"), each = 3),
                   Name = c("Washington", rep(c("Adams", "Jefferson"), 2), "Burr"),
                   start = c("1789-03-29", "1797-02-03", "1801-02-03"),
                   end = c("1797-02-03", "1801-02-03", "1809-02-03"),
                   color = c('#cbb69d', '#603913', '#c69c6e'),
                   fontcolor = c("black", "white", "black"))

shinyApp(
  ui = plotlyOutput("myVistime"),
  server = function(input, output) {
    output$myVistime <- renderPlotly({
      vistime(pres, events="Position", groups="Name") %>% plotly::layout(yaxis=list(fixedrange=TRUE))
    })
  }
)

This way, clicking on the zoom buttons only changes the x-axis.

shosaco commented 4 years ago

Thanks for the example! To be more concise, you could omit magrittr by writing

            v <- vistime(pres, events="Position", groups="Name")
            layout(v, yaxis=list(fixedrange=TRUE))
mayeulk commented 4 years ago

Still, with my code above: zooming does still somehow affect the y-axis: image In particular:

shosaco commented 4 years ago

This is true for grouped timelines, I guess it has to do with the subplots. Will look into it.

shosaco commented 4 years ago

Dear mayeulk,

today I pushed a major rework of the vistime backend which does not use subplots anymore, but distributes all events on one single Plotly plot. This way, the x-axis-Zooming is much nicer. You can install it by downloading the development version from github:

install.packages("devtools")
devtools::install_github("shosaco/vistime")

I think that solves your problem!

I should have done this change earlier, probably from the beginning, but it was a real head-cruncher...