vistime icon indicating copy to clipboard operation
vistime copied to clipboard

restrict zooming to x-dimension

Open mayeulk opened this issue 5 years ago • 6 comments

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

mayeulk avatar Mar 08 '20 22:03 mayeulk

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

shosaco avatar Mar 09 '20 08:03 shosaco

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.

mayeulk avatar Mar 09 '20 08:03 mayeulk

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))

shosaco avatar Mar 09 '20 08:03 shosaco

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

  • Some of the horizontal lines move (notice the line between Washington and Adams went down: Adams has less vertical space between lines than Burr)
  • Some brown bars are no longer between horizontal lines but over them
  • The height of the yearly vertical bars for Adams, Jefferson and Burr gets reduced (not for Washington)
  • No vertical bars are added outside of the original view frame 1790-1806

mayeulk avatar Mar 09 '20 08:03 mayeulk

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

shosaco avatar Mar 09 '20 08:03 shosaco

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...

shosaco avatar Apr 11 '20 19:04 shosaco